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 a user.
Count the number of times the user changes keys while typing the sequence. Uppercase and lowercase forms of the same letter use the same letter key, so ignore modifiers such as Shift and Caps Lock. For example, typing 'w' followed by 'W' uses the same key, while typing 'W' followed by 'E' uses different keys.
A solution with time complexity no worse than O(recording.length^2) will fit within the execution time limit.
Function
solution(recording: String[]) → intExamples
Example 1
recording = ["W","w","a","A","a","b","B"]return = 2After ignoring case, the sequence is w, w, a, a, a, b, b. The key changes from w to a and from a to b, for a total of 2.
Example 2
recording = ["w","w","A","w","a"]return = 3Ignoring case gives w, w, a, w, a. The last three boundaries change keys, so the answer is 3.
Constraints
- Every element of
recordingis one uppercase or lowercase English letter.