Count Key Changes
Learn this problemProblem statement
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 O(recording.length^2) will fit within the execution time limit.
Function
countKeyChanges(recording: char[]) → intExamples
Example 1
recording = ["W","w","a","A","a","b","B"]return = 2For recording = ['W', 'w', 'a', 'A', 'a', 'b', 'B'], 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'b'. - So, the user changed keys in the following order:
'w' -> 'a' -> 'b', and the total number of key changes is2.
Example 2
recording = ["w","w","a","w","a"]return = 3For recording = ['w', 'w', 'a', 'w', 'a'], the output should be solution(recording) = 3.
Explanation:
The user changed keys in the following order: 'w' -> 'a' -> 'w' -> 'a', and the total number of key changes is 3.
Constraints
- Input/Output
[execution time limit] 0.5 seconds (cpp)[memory limit] 1 GB[input] array.char recordingAn array of characters representing keys the user pressed. It is guaranteed that the array contains only uppercase and/or lowercase English letters. Guaranteed constraints:1 ≤ recording.length ≤ 1000.[output] integerThe number of key changes as described above.