Skip to main content
Back to problems
#925
Easy Algorithms

Long pressed name

Two Pointers String
32.8% acceptance
Feb 25, 2026
2585
408
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 friends name, with some characters (possibly none) being long pressed.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_long_pressed_name(name: String, typed: String) -> bool {
    let name: Vec<char> = name.chars().collect();
    let typed: Vec<char> = typed.chars().collect();
    let (mut i, mut j) = (0usize, 0usize);
    while j < typed.len() {
      if i < name.len() && name[i] == typed[j] {
        i += 1; j += 1;
      } else if j > 0 && typed[j] == typed[j-1] {
        j += 1;
      } else {
        return false;
      }
    }
    i == name.len()
  }
}