'''
keys: 
Solutions:
Similar:340
T:
S:
'''
from typing import Listå
import itertools

class Solution:
    # # two pointer; O(N+T) for T and O(1) for S
    # def isLongPressedName2(self, name: str, typed: str) -> bool:
    #     j = 0
    #     for c in name:
    #         if j == len(typed):
    #             return False

    #         # If mismatch...
    #         if typed[j] != c:
    #             # If it's the first char of the block, ans is False.
    #             if (j == 0) or (typed[j-1] != typed[j]):
    #                 return False

    #             # Discard all similar chars.
    #             cur = typed[j]
    #             while j < len(typed) and typed[j] == cur:
    #                 j += 1

    #             # If next isn't a match, ans is False.
    #             if j == len(typed) or typed[j] != c:
    #                 return False

    #         j += 1

    #     return True
    
    
    # group into blocks
    # O(N+T) for T and S; N and T as the lengths of name and typed
    def isLongPressedName1(self, name: str, typed: str) -> bool:
        g1 = [(k, len(list(grp))) for k, grp in itertools.groupby(name)]
        g2 = [(k, len(list(grp))) for k, grp in itertools.groupby(typed)]
        if len(g1) != len(g2):
            return False

        return all(k1 == k2 and v1 <= v2
                   for (k1,v1), (k2,v2) in zip(g1, g2))

        
    
    # concise two pointer
    def isLongPressedName(self, name: str, typed: str) -> bool:
        i = 0
        for j in range(len(typed)):
            if i < len(name) and name[i] == typed[j]:
                i += 1
            elif j == 0 or typed[j] != typed[j-1]:
                return False
        return i == len(name)
    
    
    
    
    