Long Pressed Name

easy

Check if typed string could be result of long pressing name

Long Pressed Name

Key Insight

Walk both strings: match advances both pointers. Long-press (repeat of previous) advances typed pointer only.

Step 1Setup
name="alex"
i
j
a
0
a
1
l
2
e
3
e
4
x
5
same direction

name="alex", typed="aaleex". i=0 on name, j=0 on typed.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Long Pressed Name

Walk both strings: match advances both pointers. Long-press (repeat of previous) advances typed pointer only.

  1. Setup

    name="alex", typed="aaleex". i=0 on name, j=0 on typed.

  2. First Match

    typed[0]="a" matches name[0]="a". Both advance. i=1, j=1.

  3. Long Press Detected

    typed[1]="a" ≠ name[1]="l", but matches previous typed "a" (long press). j++ only.

  4. Continue Matching

    typed[2]="l" matches name[1]="l". Both advance. Continue through e, e (long press), x.

  5. Result

    All name chars matched. Extra typed chars are valid long presses.