Long Pressed Name

Problem

Given two strings name and typed, return true if typed could be a long-pressed version of name, otherwise return false.

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

Example

Input: name = "alex", typed = "aaleex"
Output: true

The typed string "aaleex" can be formed by long pressing the characters in "alex". For example, 'a' is pressed twice, 'l' once, 'e' twice, and 'x' once. The algorithm uses two pointers to traverse both strings simultaneously. At each step, it checks if the current characters match. If they do, both pointers advance. If they don't, it checks if the current typed character matches the previous typed character, indicating a long press, and advances only the typed pointer. If neither condition holds, the function returns false. The critical moment is when the typed character differs from the name character and the previous typed character, signaling an invalid long press.

Approach

Straightforward Solution

A brute-force approach might try to count consecutive characters in both strings and compare counts, but this requires extra bookkeeping and is more complex to implement.

Core Observation

The fundamental truth is that typed must be a supersequence of name where each character in name appears in order, possibly repeated multiple times consecutively in typed.

Path to Optimal

Preview

The key insight is to use two pointers to traverse both strings simultaneously. When characters match, advance both pointers…

Full step-by-step walkthrough on Pro

Optimal Approach

Preview

Use two pointers i and j for name and typed respectively. Iterate through typed with j…

Full step-by-step walkthrough on Pro

Want the full reasoning chain?

Unlock the complete walkthrough, line-by-line analysis, and recall drill.

Unlock Pro

Time

O(n)

Each pointer advances at most the length of its respective string, resulting in a single linear pass through typed.

Space

O(1)

Only a fixed number of variables (two pointers and counters) are used, with no additional data structures proportional to input size.

Pattern Spotlight

Two Pointers (Greedy Validation)

When validating a sequence against a pattern with possible repeated characters, use two pointers to greedily match characters and allow the second pointer to advance on repeated characters, ensuring linear time verification without extra space.

Solution

Python
1class Solution:
2 def isLongPressedName(self, name: str, typed: str) -> bool:
3 i = 0
4 j = 0
5
6 while j < len(typed):
7 if i < len(name) and name[i] == typed[j]:
8 i += 1
9 j += 1
10 elif j > 0 and typed[j] == typed[j - 1]:
11 j += 1
12 else:
13 return False
14
15 return i == len(name)

Step-by-Step Solution

1

Initialize Two Pointers for Name and Typed Strings

3i = 0
4j = 0

Objective

To set up indices to traverse both strings from the start.

Key Insight

Two pointers allow simultaneous traversal of name and typed, enabling direct comparison of characters and efficient detection of long press repetitions without extra space or preprocessing.

Interview Quick-Check

Core Logic

Using two pointers i and j to track positions in name and typed respectively is the foundation for linear-time validation.

State & Boundaries

Both pointers start at 0, representing the beginning of each string.

2

Traverse Typed String to Validate Long Pressed Characters

To iterate through typed and verify it matches name with allowed long presses.

3

Confirm Complete Match of Name After Traversal

To verify that all characters in name have been matched by the end of typed traversal.

2 more steps with full analysis available on Pro.

Line Analysis

This solution has 4 Critical lines interviewers watch for.

Line 12 Critical
else:

Handle invalid character mismatch by returning false.

This immediate rejection upon mismatch is essential to prevent false positives, ensuring the algorithm only accepts typed strings that strictly follow the long press rules.

Line 7 Critical
if i < len(name) and name[i] == typed[j]:

Check if current characters in name and typed match and i is within name bounds.

This condition is the core correctness check that advances both pointers only when characters align, ensuring the typed string follows the name sequence.

Line 10 Critical
elif j > 0 and typed[j] == typed[j - 1]:

Check if current typed character equals previous typed character (long press scenario).

This check is critical to distinguish valid long presses from invalid mismatches, enabling the algorithm to accept repeated characters in typed that correspond to a single character in name.

Full line-by-line criticality + rationale for all 11 lines available on Pro.

Test Your Understanding

Why must the algorithm check if the current typed character equals the previous typed character when it doesn't match the current name character?

See the answer with Pro.

Related Problems

Two Pointers pattern

Don't just read it. Drill it.

Reconstruct Long Pressed Name from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.

Unlock the Long Pressed Name drill

or drill a free problem