Long Pressed Name (LeetCode 925): Solution and Explanation
Decide whether typed could come from typing name with some keys held down so their letters repeat.
Intuition
A long press only adds extra copies of the key that was just pressed. It never drops a letter, reorders letters or adds a different letter.
So every key in typed has exactly two honest explanations: it types the next letter of name, or it repeats the key right before it.
Walk typed with j and keep i on the next letter of name. If neither explanation fits a key, typed cannot be a long-pressed name. When typed runs out, all of name must have been typed.
Approach: Two pointers
- Set i = 0, the next letter of name we expect.
- For each j in typed: if i < len(name) and typed[j] == name[i], advance i.
- Otherwise, if j > 0 and typed[j] == typed[j - 1], the key is a long press; do nothing.
- Otherwise return false.
- After the loop, return i == len(name).
Step-by-step example
name = "alex", typed = "aaleex"
- j = 0: 'a' equals name[0], so i = 1.
- j = 1: 'a' is not name[1] = 'l', but it repeats typed[0], so it is a long press.
- j = 2: 'l' equals name[1], so i = 2. j = 3: 'e' equals name[2], so i = 3.
- j = 4: 'e' is not name[3] = 'x' but repeats the previous 'e'. j = 5: 'x' equals name[3], so i = 4.
- typed is done and i = 4 = len(name). Return true.
Code: Python, Java, C++
1class Solution:
2 def isLongPressedName(self, name: str, typed: str) -> bool:
3 i = 0
4 for j in range(len(typed)):
5 if i < len(name) and name[i] == typed[j]:
6 i += 1
7 elif j == 0 or typed[j] != typed[j - 1]:
8 return False
9 return i == len(name)1class Solution {
2 public boolean isLongPressedName(String name, String typed) {
3 int i = 0;
4 for (int j = 0; j < typed.length(); j++) {
5 if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
6 i++;
7 } else if (j == 0 || typed.charAt(j) != typed.charAt(j - 1)) {
8 return false;
9 }
10 }
11 return i == name.length();
12 }
13}1class Solution {
2public:
3 bool isLongPressedName(string name, string typed) {
4 int i = 0;
5 for (int j = 0; j < (int)typed.size(); j++) {
6 if (i < (int)name.size() && name[i] == typed[j]) {
7 i++;
8 } else if (j == 0 || typed[j] != typed[j - 1]) {
9 return false;
10 }
11 }
12 return i == (int)name.size();
13 }
14};Time and space complexity
O(n + m)
Each key of typed is looked at once, and i only moves forward through name.
O(1)
Only two indices are stored.
Other approaches
Compare run-length groups (time O(n + m), space O(n + m))
Compress both strings into (letter, count) groups. They must have the same number of groups, the same letters in order, and each typed count must be at least the name count.
Common mistakes
- Forgetting the final check. With name = "alex" and typed = "aale", every key is explained but 'x' was never typed.
- Checking the repeat against name[i - 1] instead of typed[j - 1]. Both work here, but comparing with the previous typed key states the rule directly.
- Trying the long-press branch before the match branch. For name = "leelee", the second 'e' must match name, not be treated as a held key.
- Accepting an extra different letter at the end, such as typed = "aaleexa".
Frequently asked questions
Why try the match before the long press?
If a key equals name[i], using it as that letter never hurts: any later repeat of it can still be a long press. Treating it as a long press first can leave a doubled letter in name unmatched.
Can typed be shorter than name and still be valid?
No. A long press only adds letters, so typed must contain every letter of name. The final i == len(name) check catches this.
Is this the same as checking that name is a subsequence of typed?
No. A subsequence may skip any letters, but here the only extra letters allowed are repeats of the key just pressed. "abc" is a subsequence of "axbc" but not a long press of it.