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 <= 100
s
consists of only upper case and lower case English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to count how many times the key pressed changes when typing out a text. The brute force approach is to simply go through the text one character at a time, and compare each character to the character before it.
Here's how the algorithm would work step-by-step:
def number_of_changing_keys_brute_force(text):
number_of_key_changes = 0
# Handle edge case of empty string
if not text:
return 0
# Initialize previous key
previous_key = text[0]
# Iterate from the second character
for current_index in range(1, len(text)):
current_key = text[current_index]
# Compare the current key to the previous key
if current_key != previous_key:
number_of_key_changes += 1
# Update previous key after a change
previous_key = current_key
return number_of_key_changes
The goal is to count how many times a person's finger moves to a different key while typing a word. We can efficiently solve this by just comparing each letter to the one before it, counting only when they're different.
Here's how the algorithm would work step-by-step:
def number_of_changing_keys(word):
key_change_count = 0
# If the word is empty or has only one character, no key changes occur.
if len(word) <= 1:
return key_change_count
previous_letter = word[0]
for current_index in range(1, len(word)):
current_letter = word[current_index]
# Comparing the current letter against the previous
if current_letter != previous_letter:
key_change_count += 1
# Update the previous letter to current for next comparison.
previous_letter = current_letter
return key_change_count
Case | How to Handle |
---|---|
Null or empty input string | Return 0 if the input string is null or empty, as there are no keys to change. |
String with a single character | Return 0 if the string has only one character, as there are no adjacent characters to compare. |
String with all identical characters | Return string length - 1 because every adjacent key would be a change. |
String with only two characters that are different | Return 1 if the string has exactly two different characters. |
Very long input string | Iterate through the string linearly with O(n) time complexity. |
String with mixed case characters | Convert the entire string to lowercase or uppercase before processing to ensure case-insensitive comparison. |
String containing non-alphanumeric characters | Handle non-alphanumeric characters by either ignoring them or converting them to a uniform representation before processing. |
Integer overflow potential with extremely long sequences | The number of key changes is bounded by the string length so an int should be sufficient. |