Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.
Example 1:
Input: s = "aAbBcC" Output: 2 Explanation: 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 Explanation: 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 <= 100s consists of only upper case and lower case English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"aAbBcC"
"AaAaAaaA"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3019: Number of Changing Keys
class Solution {
public int countKeyChanges(String s) {
int ans = 0;
for (int i = 1; i < s.length(); ++i) {
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3019: Number of Changing Keys
func countKeyChanges(s string) (ans int) {
s = strings.ToLower(s)
for i, c := range s[1:] {
if byte(c) != s[i] {
ans++
}
}
return
}
# Accepted solution for LeetCode #3019: Number of Changing Keys
class Solution:
def countKeyChanges(self, s: str) -> int:
return sum(a != b for a, b in pairwise(s.lower()))
// Accepted solution for LeetCode #3019: Number of Changing Keys
impl Solution {
pub fn count_key_changes(s: String) -> i32 {
let s = s.to_lowercase();
let bytes = s.as_bytes();
let mut ans = 0;
for i in 1..bytes.len() {
if bytes[i] != bytes[i - 1] {
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #3019: Number of Changing Keys
function countKeyChanges(s: string): number {
s = s.toLowerCase();
let ans = 0;
for (let i = 1; i < s.length; ++i) {
if (s[i] !== s[i - 1]) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.