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.
Example
For recording = ['w', 'w', 'a', 'A', 'a', 'b', 'B'], the output should be solution(recording) = 2.
Explanation:
- 这道题的核心是把大小写字母映射到同一个按键:只要当前字符和前一个字符忽略大小写后相同,就说明用户没有换键;否则答案加一。做法非常直接,遍历数组时用一个变量记录上一个按键对应的“小写 / 统一形式”,每次将当前字符转成同一标准后比较即可,时间复杂度 O(n),空间复杂度 O(1)。题目给出的示例中,像 w 和 W、a 和 A 都算同一键,而 w 到 a、a 到 b 才算发生按键切换,因此最终统计的是“相邻不同键”的次数。