LeetCode 925. Long Pressed Name



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.

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.

Example 3:

Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.


  • 1 <= name.length <= 1000
  • 1 <= typed.length <= 1000
  • name and typed contain only lowercase English letters.


Break letters in name and typed in groups and compare if the groups lengths are the same and whether typed letter groups contain more letters than name letter groups.

Python Solution

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        groups_name = []
        groups_name_count = []
        prev = None        
        for c in name:
            if c != prev:
                prev = c
                groups_name_count[-1] = groups_name_count[-1] + 1

        groups_typed = []
        prev = None
        groups_typed_count = []
        for c in typed:
            if c != prev:
                prev = c                 
                groups_typed_count[-1] = groups_typed_count[-1] + 1

        if groups_name != groups_typed:
            return False
        for i, j in zip(groups_name_count, groups_typed_count):
            if i > j:
                return False
        return True
  • Time Complexity: O(N).
  • Space Complexity: O(N).

Leave a Reply

Your email address will not be published. Required fields are marked *