Number of Changing Keys - Problem
You are given a 0-indexed string s typed by a user. Changing a key is defined as using a key different from the last used key.
For example, s = "ab" has a change of a key while s = "bBBb" does not have any.
Return the number of times the user had to change the key.
Note: Modifiers like shift or caps lock won't be counted in changing the key. That is, if a user typed the letter 'a' and then the letter 'A' then it will not be considered as a changing of key.
Input & Output
Example 1 — Basic Case with Changes
$
Input:
s = "aAbBcC"
›
Output:
2
💡 Note:
Changes occur at positions 2 (A→b) and 4 (B→c). Characters 'a'/'A', 'b'/'B', and 'c'/'C' are the same keys respectively.
Example 2 — No Changes
$
Input:
s = "AaAaAaaA"
›
Output:
0
💡 Note:
All characters represent the same key 'a', so no key changes are needed regardless of case.
Example 3 — All Different Keys
$
Input:
s = "abcd"
›
Output:
3
💡 Note:
Every adjacent pair represents different keys: a→b, b→c, c→d, resulting in 3 changes.
Constraints
- 1 ≤ s.length ≤ 100
- s consists of only English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code