Long Pressed Name - Problem

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 friend's name, with some characters (possibly none) being long pressed.

Input & Output

Example 1 — Basic Long Press
$ Input: name = "alex", typed = "aaleex"
Output: true
💡 Note: The 'a' and 'e' were long pressed, resulting in extra characters, but the sequence matches the name
Example 2 — Wrong Character
$ Input: name = "saeed", typed = "ssaaedd"
Output: false
💡 Note: The 'e' in typed was long pressed, but we're missing one 'e' that should appear after 'a'
Example 3 — Perfect Match
$ Input: name = "leelee", typed = "lleeelee"
Output: true
💡 Note: All characters match with some long presses on 'l' and 'e'

Constraints

  • 1 ≤ name.length ≤ 1000
  • 1 ≤ typed.length ≤ 1000
  • name and typed consist of only lowercase English letters

Visualization

Tap to expand
Long Pressed Name Problem OverviewFriend's Name"alex"What Was Typed"aaleex"Long Press Analysisa → aa (long pressed)e → ee (long pressed)Result: trueTyping is possible!
Understanding the Visualization
1
Input
Friend's name and what was actually typed
2
Process
Check if typed string could result from long-pressing keys
3
Output
Return true if typing is possible, false otherwise
Key Takeaway
🎯 Key Insight: Use two pointers to match characters while allowing repetitions from long-pressed keys
Asked in
Google 15 Facebook 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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