Problem · Array

Count Key Changes

Learn this problem
EasyMeta logoMetaINTERNOA
See Meta hiring insights

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

Examples

Example 1

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

After 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 = 3

Ignoring case gives w, w, a, w, a. The last three boundaries change keys, so the answer is 3.

Constraints

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

More Meta problems

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