925. Long Pressed Name
Problem Description
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.
Constraints:
1 <= name.length, typed.length <= 1000nameandtypedcontain only lowercase English letters.
Solution
Python Solution
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i = j = 0
while j < len(typed):
if i < len(name) and name[i] == typed[j]:
i += 1
j += 1
elif j > 0 and typed[j] == typed[j - 1]:
j += 1
else:
return False
return i == len(name)
Time Complexity: O(n + m)
We need to traverse both strings once.
Space Complexity: O(1)
We only use two pointers.
Java Solution
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
while (j < typed.length()) {
if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
i++;
j++;
} else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
j++;
} else {
return false;
}
}
return i == name.length();
}
}
Time Complexity: O(n + m)
We need to traverse both strings once.
Space Complexity: O(1)
We only use two pointers.
C++ Solution
class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, j = 0;
while (j < typed.length()) {
if (i < name.length() && name[i] == typed[j]) {
i++;
j++;
} else if (j > 0 && typed[j] == typed[j - 1]) {
j++;
} else {
return false;
}
}
return i == name.length();
}
};
Time Complexity: O(n + m)
We need to traverse both strings once.
Space Complexity: O(1)
We only use two pointers.
JavaScript Solution
/**
* @param {string} name
* @param {string} typed
* @return {boolean}
*/
var isLongPressedName = function(name, typed) {
let i = 0, j = 0;
while (j < typed.length) {
if (i < name.length && name[i] === typed[j]) {
i++;
j++;
} else if (j > 0 && typed[j] === typed[j - 1]) {
j++;
} else {
return false;
}
}
return i === name.length;
};
Time Complexity: O(n + m)
We need to traverse both strings once.
Space Complexity: O(1)
We only use two pointers.
C# Solution
public class Solution {
public bool IsLongPressedName(string name, string typed) {
int i = 0, j = 0;
while (j < typed.Length) {
if (i < name.Length && name[i] == typed[j]) {
i++;
j++;
} else if (j > 0 && typed[j] == typed[j - 1]) {
j++;
} else {
return false;
}
}
return i == name.Length;
}
}
Time Complexity: O(n + m)
We need to traverse both strings once.
Space Complexity: O(1)
We only use two pointers.