Problem · Array

Count Key Changes

Learn this problem
EasyHudson River Trading logoHudson River TradingFULLTIMENEW GRADINTERNOA

Problem 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[]) → int

Examples

Example 1

recording = ["W","w","a","A","a","b","B"]return = 2

For 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 is 2.

Example 2

recording = ["w","w","a","w","a"]return = 3

For 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 recording An 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] integer The number of key changes as described above.

More Hudson River Trading problems

drafts saved locally
public int countKeyChanges(char[] recording) {
    // write your code here
}
recording["W","w","a","A","a","b","B"]
expected2
checking account