Long Pressed Name

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Long Pressed Name

Approach

Use two pointers, one for the name and one for typed. If characters match, advance both. If the typed character matches the previous typed character, it is a long press, so advance only the typed pointer. Otherwise return false. Verify all name characters were consumed.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Same Direction)

Why It Works

Long presses only repeat the previous character. By checking if unmatched typed characters are repeats of the prior character, we distinguish valid long presses from mistyped characters.

Updated Feb 2026