[
string
groupby
two pointers
]
Leetcode 0925 Long Pressed Name
Problem statement
https://leetcode.com/problems/long-pressed-name/
Solution
One way to solve is to use two pointers approach. Another way is to use groupby and then check that for each group symbols are equal and we have correct inequality.
Complexity
Time and space complexity is O(m + n)
, where m
is the length of name
and n
is the length of typed
.
Code
class Solution:
def isLongPressedName(self, name, typed):
def group(s):
return [(i, len(list(j))) for i, j in groupby(s)]
g1, g2 = group(name), group(typed)
if len(g1) != len(g2): return False
for (l1, f1), (l2, f2) in zip(g1, g2):
if l1 != l2 or f1 > f2: return False
return True