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 was not in the typed output.
Constraints:
1 <= name.length, typed.length <= 1000name and typed consist of only lowercase English letters.Problem Overview: You are given two strings: name and typed. The string typed represents what appears on the keyboard when someone types name, but some characters might be long‑pressed. A long press repeats the same character multiple times. The task is to verify whether typed could have been produced from name while preserving the character order.
Approach 1: Two Pointers Technique (O(n) time, O(1) space)
This is the optimal approach and the one interviewers expect. Use two pointers: one for name and one for typed. Iterate through typed while matching characters with name. When both pointers match, move both forward. If they don't match but the current typed character equals the previous character in typed, treat it as a long press and advance only the typed pointer. If neither condition holds, the string is invalid. This works because long presses only repeat the previous character without changing order. The scan runs once across the strings, giving O(n) time complexity with O(1) extra space.
This pattern is common in Two Pointers problems where two sequences must be compared while preserving order. Since you only track indices and a previous character check, the implementation stays simple and memory efficient.
Approach 2: Frequency Count Comparison (O(n) time, O(1) space)
Another way to reason about the problem is by grouping consecutive characters in both strings. For example, name = "alex" becomes groups like a(1), l(1), e(1), x(1). If typed = "aaleex", the groups are a(2), l(1), e(2), x(1). Iterate through both strings and count the length of each consecutive character block. For the strings to be valid, the characters must match and the count in typed must be greater than or equal to the count in name. If typed has fewer occurrences of a character than name, it means a required character was skipped.
This approach relies on basic String processing and run-length counting. Complexity remains O(n) because each character is processed once while forming groups. Space complexity stays O(1) since counts are tracked using simple variables.
Recommended for interviews: The Two Pointers solution is the expected answer. It demonstrates strong control over sequential comparison problems and minimal memory usage. The frequency group method is also valid but slightly more verbose. Showing the brute reasoning first (character repetition groups) and then optimizing to the pointer scan signals strong problem‑solving ability.
The two-pointer technique involves iterating through both the 'name' and 'typed' strings simultaneously. We use two indices, one for each string. The idea is to compare the characters between the two strings and verify if the characters in 'typed' match the characters in 'name'. If the character in 'name' matches the character in 'typed', move both pointers forward. If it's a long press (i.e., the current and previous 'typed' characters are the same), increment the 'typed' pointer. If the characters do not match and it's not a long press scenario, return false.
The function iterates over the 'name' and 'typed' strings. If a character from 'name' matches a character from 'typed', it advances both pointers. If there's a discrepancy, it checks if it can be justified by a long press. If not, it returns false.
Time Complexity: O(n + m), where n is the length of 'name' and m is the length of 'typed'.
Space Complexity: O(1), as no extra space is used.
In this approach, we utilize frequency counting. We traverse both strings to collect segments of consecutive repeating characters. For each segment in 'name', ensure there is a corresponding segment in 'typed' with equal or greater length, indicating potential long presses.
The function checks segments of characters in 'name' and ensures corresponding segments in 'typed' have lengths greater than or equal to those in 'name'. If any segment in 'name' is longer than its counterpart in 'typed', it returns false.
Time Complexity: O(n + m)
Space Complexity: O(1)
We use two pointers i and j to point to the first character of the strings typed and name respectively, and then start traversing. If typed[j] is not equal to name[i], it means the two strings do not match, and we directly return False. Otherwise, we find the next position of the continuous identical characters, denoted as x and y respectively. If x - i > y - j, it means the number of characters in typed is less than the number of characters in name, and we directly return False. Otherwise, we update i and j to x and y respectively, continue traversing, until i and j have traversed name and typed respectively, and return True.
The time complexity is O(m + n), where m and n are the lengths of the strings name and typed respectively. The space complexity is $O(1)`.
| Approach | Complexity |
|---|---|
| Two Pointers Technique | Time Complexity: O(n + m), where n is the length of 'name' and m is the length of 'typed'. |
| Frequency Count Comparison | Time Complexity: O(n + m) |
| Two Pointers | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two Pointers Technique | O(n) | O(1) | Best general solution for comparing sequential strings with possible repeats |
| Frequency Count (Character Groups) | O(n) | O(1) | Useful when reasoning about consecutive character blocks or explaining the long‑press rule clearly |
Faulty Keyboard | long pressed name | Leetcode 925 Solution in Hindi • Pepcoding • 63,658 views views
Watch 9 more video solutions →Practice Long Pressed Name with our built-in code editor and test cases.
Practice on FleetCode