Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.
You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Example 1:
Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it was not in the typed output.
Constraints:
1 <= name.length, typed.length <= 1000name and typed consist of only lowercase 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:
The brute force approach to this problem involves meticulously checking if each character in the 'typed' name can possibly come from the original 'name'. We will look at 'typed' character by character and try to account for each one using the original 'name'.
Here's how the algorithm would work step-by-step:
def is_long_pressed_name(name, typed):
name_index = 0
typed_index = 0
while typed_index < len(typed):
if name_index < len(name) and name[name_index] == typed[typed_index]:
name_index += 1
typed_index += 1
# Check for long press
elif typed_index > 0 and typed[typed_index] == typed[typed_index - 1]:
typed_index += 1
# If chars don't match and it's not a long press, return False
else:
return False
# Check if we've reached the end of the original name
if name_index != len(name):
return False
return TrueThe key idea is to compare the typed name and the original name character by character, checking for long presses. We walk through both names at the same time, handling cases where characters are repeated (long pressed) in the typed name.
Here's how the algorithm would work step-by-step:
def is_long_pressed_name(name, typed):
name_index = 0
typed_index = 0
while typed_index < len(typed):
if name_index < len(name) and name[name_index] == typed[typed_index]:
name_index += 1
typed_index += 1
elif typed_index > 0 and typed[typed_index] == typed[typed_index - 1]:
# This handles long presses of characters in the typed name.
typed_index += 1
else:
return False
# Ensure all characters in name have been matched.
if name_index != len(name):
return False
return True| Case | How to Handle |
|---|---|
| name is null or empty | Return false immediately as there is nothing to compare against. |
| typed is null or empty | Return false immediately as there is nothing to compare against. |
| name is longer than typed | Return false, since 'typed' cannot be a long-pressed version of a longer 'name'. |
| name and typed are identical | Return true, as 'typed' is a valid long-pressed version of 'name'. |
| name contains repeating characters at the end, and typed has excessive repetitions of that last character | The main loop and repetition check should properly handle the trailing character repetitions. |
| typed contains a character that is not in name | Return false, as typed cannot contain characters not present in name. |
| name contains all same character and typed contains all same character (but different count) | The character counting logic ensures 'typed' contains at least the same number of repeating characters as 'name'. |
| Consecutive repeated characters in name are not repeated in typed (e.g. name="alex", typed="ale") | Return false, the typed string is missing repetitions of a required character. |