Problem statement

https://binarysearch.com/problems/Stuck-Keyboard/

Solution

Equal to Leetcode 0925 Long Pressed Name.

Complexity

It is O(m + n) for time and space.

Code

class Solution:
    def solve(self, typed, name):
        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