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 "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
๐ŸŽน The Keyboard SymphonyOriginal Score (name):โ™ชaโ™ซlโ™ชeโ™ซxPerformance (typed):โ™ชaโ™ชasticky!โ™ซlโ™ชeโ™ชesticky!โ™ซxTwo Conductors Approach:๐Ÿ‘จโ€๐ŸŽผ Score Conductor (i): Follows original composition๐Ÿ‘ฉโ€๐ŸŽผ Performance Conductor (j): Follows live performanceโœ… When notes match: Both conductors advance๐Ÿ”„ When sticky key detected: Only performance conductor advancesโŒ When notes differ (not sticky): Performance is invalid๐ŸŽฏ Symphony Complete!Performance matches original with sticky keys
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

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n)

Need to store all possible generated strings in memory

n
2n
โš  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
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 22
78.4K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen