Answer the given question using C++, also ensure that the end user is able to input the…
- User
Answer the given question using C++, also ensure that the end user is able to input the test case: You are given an array of uppercase and lowercase English letters recording representing a sequence of letters typed by the user. Your task is to count the number of times that the user changed keys while typing the sequence, considering that the uppercase and lowercase letters for a given letter require the user to press same letter key (ignoring modifiers like Shift or Caps Lock). For example, typing "w' and "w' require the user to press the same key, whereas typing "w' and 'E' or typing "w' and e' require the user to change keys. Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than (recording. length^2) will fit within the execution time limit. Example For recording = ['w', "w". a, "A". a', "b, *B'1, the output should be solution (recording) = 2. Explanation: Typing w and "w' require the same key "w Typing 'A' and 'a' require the same key a' Typing 'b' and 'B' require the same key 6 So, the user changed keys in the following order: ‘w’→ "a' →> "b", and the total
- Assistant
To solve this problem, we need to determine how many times a user switches keys while typing a sequence of letters, considering uppercase and lowercase letters of the same character as the same key. We can achieve this by converting each character to lowercase (to represent the key pressed) and then counting the number of times the key changes in the sequence.
Here is the C++ code that accomplishes this task:
#include <iostream> #include <vector> #include <cctype> int solution(std::vector<char>& recording) { int n = recording.size(); std::vector<char> keys; for(int i = 0; i < n; i++) { keys.push_back(std::tolower(recording[i])); } int count = 0; for(int i = 1; i < n; i++) { if(keys[i] != keys[i-1]) { count++; } } return count; } int main() { std::vector<char> recording; int n; std::cout << "Enter the number of letters: "; std::cin >> n; std::cout << "Enter the letters (one per line): " << std::endl; for(int i = 0; i < n; i++) { char ch; std::cin >> ch; recording.push_back(ch); } int result = solution(recording); std::cout << "Number of key changes: " << result << std::endl; return 0; }Explanation:
-
Input Reading:
- The program first asks the user to input the number of letters (
n). - It then reads
nletters from the user and stores them in therecordingvector.
- The program first asks the user to input the number of letters (
-
Processing:
- Converts each character in
recordingto lowercase to represent the key pressed. - Counts the number of times the key changes by comparing each key with the previous one in the sequence.
- Converts each character in
-
Output:
- Outputs the total number of key changes detected in the sequence.
Sample Input and Output:
For the example provided:
Enter the number of letters: 7 Enter the letters (one per line): w w a A a b B Number of key changes: 2This output matches the expected result of
solution(recording) = 2, indicating that the user changed keys two times in the sequence.How to Run the Program:
- Copy the code into a file named, for example,
key_changes.cpp. - Compile the code using a C++ compiler, e.g.,
g++ key_changes.cpp -o key_changes. - Run the executable, e.g.,
./key_changes. - Input the number of letters and the sequence when prompted.
Note:
- This solution has a time complexity of O(n), where n is the number of letters in the recording, complying with the requirement that the time complexity should not be worse than O(n^2).
- The program ensures that the end user can input the test case directly, making it interactive and easy to test with different inputs.
-