Domain 1EasyCORE

Long Pressed Name

The problem asks us to determine if a given string `typed` could be a result of typing another string `name` where some characters might have been long-pressed. In other words, we need to check if `typed` is a possible "long-pressed" version of `name`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Two Pointers#String

925. Long Pressed Name

Model: qwen2.5-coder:14b
Topics: Two Pointers,String
FAANG asked: 1
Acceptance rate: 37.5%


Problem Breakdown

The problem asks us to determine if a given string typed could be a result of typing another string name where some characters might have been long-pressed. In other words, we need to check if typed is a possible “long-pressed” version of name.

Inputs:

  • name: A string representing the original name.
  • typed: A string representing the typed output.

Outputs:

  • A boolean value indicating whether typed could be a long-pressed version of name.

Implicit Constraints/Edge Cases:

  • If typed is shorter than name, it cannot be a valid long-pressed version.
  • If typed contains characters not present in name, it’s invalid.
  • Characters that appear multiple times in typed must have appeared at least as many times consecutively in name.

Intuition

A brute-force approach would involve generating all possible long-pressed versions of name and checking if any match typed. However, this is computationally expensive with a time complexity of O(2^n) due to the exponential growth of possibilities.

The key insight is that for each character in typed, it must either match the current character in name or be a long-pressed version of the previous character. This allows us to use a two-pointer technique to traverse both strings efficiently.

Approach

  1. Initialize two pointers, i and j, to traverse name and typed, respectively.
  2. Iterate through both strings:
    • If characters at name[i] and typed[j] match, move both pointers forward (i++, j++).
    • If they don’t match, check if the current character in typed is the same as the previous character in typed. If so, move only the typed pointer forward (j++). Otherwise, return False.
  3. After the loop, ensure that all characters in name have been matched by checking if i has reached the end of name.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the length of name and m is the length of typed. We traverse both strings once.
  • Space Complexity: O(1). We use only a constant amount of extra space for pointers.

Solution

def isLongPressedName(name: str, typed: str) -> bool:
    i, j = 0, 0
    while j < len(typed):
        if i < len(name) and name[i] == typed[j]:
            i += 1
            j += 1
        elif j > 0 and typed[j] == typed[j - 1]:
            j += 1
        else:
            return False
    return i == len(name)

Edge Cases Handled

  • If typed is shorter than name, the loop will not complete, and i will not reach the end of name, returning False.
  • If typed contains characters not present in name, the mismatch check (name[i] != typed[j]) will fail at some point.
  • If typed has extra characters that are not long-pressed versions of consecutive characters in name, the mismatch check will also fail.