Long Pressed Name - Problem
Long Pressed Name - Your friend is typing his name on a keyboard, but sometimes keys get stuck! ๐น
When your friend types his name, occasionally a key might get long pressed, causing a character to be typed multiple times in a row. For example, if his name is
Your Task: Given the original
Key Rules:
โข Characters must appear in the same order as the original name
โข Each character can be repeated 1 or more times (long pressed)
โข No extra characters can be added that weren't in the original name
Example:
When your friend types his name, occasionally a key might get long pressed, causing a character to be typed multiple times in a row. For example, if his name is
"alex" but the 'l' key gets stuck, the typed result might be "allex" or even "alllex".Your Task: Given the original
name and the typed string, determine if it's possible that the typed string was created by long-pressing some (or none) of the keys while typing the original name.Key Rules:
โข Characters must appear in the same order as the original name
โข Each character can be repeated 1 or more times (long pressed)
โข No extra characters can be added that weren't in the original name
Example:
name = "alex", typed = "aaleex" โ True (both 'a' and 'e' were long pressed) Input & Output
example_1.py โ Python
$
Input:
name = "alex", typed = "aaleex"
โบ
Output:
true
๐ก Note:
The 'a' key and 'e' key were long pressed, creating extra characters. The sequence matches with long presses: a(long) + l + e(long) + x
example_2.py โ Python
$
Input:
name = "saeed", typed = "ssaaedd"
โบ
Output:
false
๐ก Note:
The 'e' key was long pressed, but there's a missing 'e' character. We have 'eed' in name but only 'edd' pattern in typed after accounting for long presses
example_3.py โ Python
$
Input:
name = "leelee", typed = "lleeelee"
โบ
Output:
true
๐ก Note:
Multiple long presses occurred: the first 'l' and middle 'e' characters were long pressed, but the sequence order remains correct
Visualization
Tap to expand
Understanding the Visualization
1
Set the Stage
Place two conductors - one following the original score (name), one following the performance (typed)
2
Synchronize the Music
When both conductors point to matching notes, they move forward together in harmony
3
Handle Sticky Keys
When notes don't match but the performer plays the same note as before, it's a sticky key - only the performance conductor advances
4
Complete the Symphony
The piece is valid if we reach the end of the original score and any remaining performance notes are just sticky key repeats
Key Takeaway
๐ฏ Key Insight: By using two pointers that move at different paces, we can efficiently validate if a typed string matches the original with possible long-pressed characters, all in a single pass with O(n+m) time complexity.
Time & Space Complexity
Time Complexity
O(2^n)
For each character, we can choose to repeat it 1 to m times, leading to exponential combinations
โ Quadratic Growth
Space Complexity
O(2^n)
Need to store all possible generated strings in memory
โ Quadratic Space
Constraints
- 1 โค name.length โค 1000
- 1 โค typed.length โค 1000
- name and typed consist of only lowercase English letters
- The typed string length is at least equal to name length
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code