Problem · Array

Count Key Changes

Learn this problem
EasyTiktokNEW GRADINTERNOA
See Tiktok hiring insights

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 the 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: String[]) → int

Examples

Example 1

recording = ["w", "W", "e", "E"]return = 1

The transition from "w" to "W" stays on the same letter key. The transition from "W" to "e" changes keys, and "e" to "E" stays on the same key. The total is 1.

Example 2

recording = ["a", "b", "C", "c", "D"]return = 3

The key changes are a → b, b → C, and c → D. The pair C → c uses the same key, so the answer is 3.

Example 3

recording = ["Z", "z", "Z"]return = 0

Every entry is a case variant of the letter z, so every transition uses the same key and the result is 0.

Constraints

  • Every element of recording is one uppercase or lowercase English letter.

More Tiktok problems

drafts saved locally
public int countKeyChanges(String[] recording) {
    // write your code here
}
recording["w", "W", "e", "E"]
expected1
checking account