LeetCode Solutions
925. Long Pressed Name
Time: Space:
class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0;
for (int j = 0; j < typed.length(); ++j)
if (i < name.length() && name[i] == typed[j])
++i;
else if (j == 0 || typed[j] != typed[j - 1])
return false;
return i == name.length();
}
};
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0;
for (int j = 0; j < typed.length(); ++j)
if (i < name.length() && name.charAt(i) == typed.charAt(j))
++i;
else if (j == 0 || typed.charAt(j) != typed.charAt(j - 1))
return false;
return i == name.length();
}
}
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i = 0
for j, t in enumerate(typed):
if i < len(name) and name[i] == t:
i += 1
elif j == 0 or t != typed[j - 1]:
return False
return i == len(name)