KatInterview

961. Long Pressed Name

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to determine if a typed string could be a result of typing a name string where some keys might have been "long pressed". A long press on a key means the corresponding character is typed one or more times. The core requirements are:

Solution

Long Pressed Name in Python

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        i = 0  # Pointer for name
        j = 0  # Pointer for typed

        while j < len(typed):
            if i < len(name) and name[i] == typed[j]:
                # Characters match, advance both pointers.
                i += 1
                j += 1
            elif j > 0 and typed[j] == typed[j-1]:
                # The current character in typed is a long press of the previous character.
                # Consume this character by advancing only the j pointer.
                j += 1
            else:
                # Mismatch: typed[j] is not name[i] and not a long press.
                return False
        
        # After iterating through typed, we must have also completed name.
        return i == len(name)

Long Pressed Name in Java

class Solution {
    public boolean isLongPressedName(String name, String typed) {
        int i = 0; // Pointer for name
        int j = 0; // Pointer for typed

        while (j < typed.length()) {
            if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
                // Characters match, advance both pointers.
                i++;
                j++;
            } else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
                // The current character in typed is a long press of the previous character.
                // Consume this character by advancing only the j pointer.
                j++;
            } else {
                // Mismatch: typed.charAt(j) is not name.charAt(i) and not a long press.
                return false;
            }
        }

        // After iterating through typed, we must have also completed name.
        return i == name.length();
    }
}

Long Pressed Name in C++

class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int i = 0; // Pointer for name
        int j = 0; // Pointer for typed

        while (j < typed.length()) {
            if (i < name.length() && name[i] == typed[j]) {
                // Characters match, advance both pointers.
                i++;
                j++;
            } else if (j > 0 && typed[j] == typed[j - 1]) {
                // The current character in typed is a long press of the previous character.
                // Consume this character by advancing only the j pointer.
                j++;
            } else {
                // Mismatch: typed[j] is not name[i] and not a long press.
                return false;
            }
        }

        // After iterating through typed, we must have also completed name.
        return i == name.length();
    }
};

Long Pressed Name in TypeScript

function isLongPressedName(name: string, typed: string): boolean {
    let i = 0; // Pointer for name
    let j = 0; // Pointer for typed

    while (j < typed.length) {
        if (i < name.length && name[i] === typed[j]) {
            // Characters match, advance both pointers.
            i++;
            j++;
        } else if (j > 0 && typed[j] === typed[j - 1]) {
            // The current character in typed is a long press of the previous character.
            // Consume this character by advancing only the j pointer.
            j++;
        } else {
            // Mismatch: typed[j] is not name[i] and not a long press.
            return false;
        }
    }

    // After iterating through typed, we must have also completed name.
    return i === name.length;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.