↓Skip to main content
  1. LeetCode/

LeetCode 925: Long Pressed Name

·2 mins· ·
LeetCode Easy Two-Pointers String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-25

  • Submissions: Runtime Error ×2 → Internal Error → Wrong Answer ×5 → Accepted

Approach
#

One pointer per string (Two Pointers Template 2), looping over typed:

  • If typed[j] equals name[i], it matches and i advances.
  • Otherwise it must be a long press, typed[j] == typed[j - 1]; if not, fail.

Finally check that i reached the end of name, so typed did not stop partway.

My Attempts
#

This took 9 submissions. The middle versions looped only while both pointers were in range, and on a mismatch did i += 1 before comparing name[i]:

  • i was not bounds-checked after the increment: name = "b", typed = "a" raised an IndexError.
  • The loop stopped once name ran out, so the rest of typed was never checked.

Looping over typed, where every character must match or be a long press, finally passed.

Solution
#

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        i = 0
        for j in range(len(typed)):
            if i < len(name) and name[i] == typed[j]:
                i += 1
            elif j == 0 or typed[j] != typed[j - 1]:
                return False
        return i == len(name)

Complexity
#

Time O(n + m), space O(1).

Mistakes to Avoid
#

  • Loop over typed so that every extra character in it is checked.
  • At the end confirm all of name was matched.

Interview Explanation
#

With two pointers driven by typed: a character matching the current name character advances both, otherwise it must repeat the previous typed character (a long press) or we fail; finally name must be fully matched. Time O(n + m), space O(1).

Related

LeetCode 917: Reverse Only Letters
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 917 note: two pointers each skip non-letters and swap when both point at letters; convert the string to a list first
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 844: Backspace String Compare
·2 mins
LeetCode Easy Two-Pointers String Stack Simulation
LeetCode 844 note: simulate backspaces with a stack; for O(1) space, walk from the end and count how many characters each # skips
LeetCode 680: Valid Palindrome II
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 680 note: at the first mismatch the only options are deleting the left or the right character; check whether either remainder is a palindrome
LeetCode 696: Count Binary Substrings
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 696 note: split the string into runs of equal characters; each adjacent pair of runs contributes min(previous, current) substrings
LeetCode 541: Reverse String II
·1 min
LeetCode Easy Two-Pointers String
LeetCode 541 note: in every block of 2k reverse the first k; when fewer than k remain reverse them all, which slicing handles automatically