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.
More Meta problems
- Count Fully Used BatteriesOA · Seen Jul 2026
- Minimum Operations for a Stepwise SequenceOA · Seen Jul 2026
- Most Frequent Reduced DigitOA · Seen Jul 2026
- Plan a Minimum-Cost Round TripONSITE INTERVIEW · Seen Jul 2026
- Merge Three Sorted ArraysPHONE SCREEN · Seen May 2026
- Highest Rating Price RatioOA · Seen Mar 2026
- Diagonal Traverse (for E4 ;)PHONE SCREEN · Seen Mar 2025
- Find Peak ElementPHONE SCREEN · Seen Mar 2025