LeetCode Problem Workspace
Number of Changing Keys
Count how many times a user switches keys in a typed string, ignoring shift and caps lock variations in characters.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Count how many times a user switches keys in a typed string, ignoring shift and caps lock variations in characters.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
This problem asks you to determine how many times a user changed keys when typing a string, treating letters with different cases as the same key. The optimal approach converts all characters to lowercase and iterates through the string to count only real key switches. This leverages a string-driven solution strategy for fast and accurate calculation of key changes.
Problem Statement
You are given a string s representing the keys typed by a user. A change of key occurs when the user types a letter different from the last typed letter, ignoring case differences. For example, typing 'a' followed by 'A' does not count as a key change.
Return the total number of key changes in the string. Modifiers like shift or caps lock do not count, so only the switch between distinct letters in a case-insensitive way is considered a change.
Examples
Example 1
Input: s = "aAbBcC"
Output: 2
From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted. From s[1] = 'A' to s[2] = 'b', there is a change of key. From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted. From s[3] = 'B' to s[4] = 'c', there is a change of key. From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted.
Example 2
Input: s = "AaAaAaaA"
Output: 0
There is no change of key since only the letters 'a' and 'A' are pressed which does not require change of key.
Constraints
- 1 <= s.length <= 100
- s consists of only upper case and lower case English letters.
Solution Approach
Normalize the string
Convert all characters in s to lowercase to ignore shift or caps lock effects, ensuring that subsequent comparisons detect actual key changes rather than case variations.
Iterate and compare adjacent keys
Loop through the string from the second character, comparing each character with the previous one. Increment a counter whenever the current character differs from the last recorded key.
Return the count
After processing the entire string, return the counter representing the total number of times the user switched keys while typing.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) because we traverse the string once. Space complexity is O(1) if we modify in place or O(n) if creating a lowercase copy.
What Interviewers Usually Probe
- Expecting an O(n) string traversal solution using simple comparisons.
- Checking for correct handling of uppercase and lowercase letters as the same key.
- Looking for clarity in defining what counts as a key change.
Common Pitfalls or Variants
Common pitfalls
- Counting changes when only the letter case differs, which is not a key change.
- Forgetting to compare each character with the previous one after normalization.
- Returning the length minus one or other off-by-one errors in counting changes.
Follow-up variants
- Count key changes including numeric and symbol keys, still ignoring modifiers.
- Track positions of each key change along with the count.
- Consider key changes only for consecutive different letters ignoring vowels or consonants.
FAQ
Does typing 'a' then 'A' count as a key change?
No, the problem treats letters with different cases as the same key, so this does not increment the change count.
What is the recommended approach pattern for this problem?
Use a string-driven solution strategy: normalize all characters to lowercase and compare each with the previous key.
Can I use extra space for a lowercase copy of the string?
Yes, using O(n) space for a lowercase copy is fine, though in-place modification reduces space to O(1).
How do I handle a string with only one character?
A single character does not involve a key change, so the count is 0.
What is the time complexity for the Number of Changing Keys solution?
It is O(n) because each character is visited once for comparison, regardless of string length up to 100.
Solution
Solution 1: Single Pass
We can traverse the string, each time checking whether the lowercase form of the current character is the same as the lowercase form of the previous character. If they are different, it means that the key has been changed, and we can increment the answer accordingly.
class Solution:
def countKeyChanges(self, s: str) -> int:
return sum(a != b for a, b in pairwise(s.lower()))Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward