Skip to content

Latest commit

 

History

History
38 lines (35 loc) · 1.02 KB

809.md

File metadata and controls

38 lines (35 loc) · 1.02 KB

809. Expressive Words

Solution 1

class Solution(object):
    def expressiveWords(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: int
        """
        def judge(s, word):
            p1 = p2 = 0
            while p1 < len(s) and p2 < len(word):
                if s[p1] != word[p2]:
                    return False
                c = s[p1]
                cnt1 = 0
                while p1 < len(s) and s[p1] == c:
                    cnt1 += 1
                    p1 += 1
                cnt2 = 0
                while p2 < len(word) and word[p2] == c:
                    cnt2 += 1
                    p2 += 1
                if cnt1 < cnt2:
                    return False
                if cnt1 != cnt2 and cnt1 < 3:
                    return False
            return p1 == len(s) and p2 == len(word)
        
        ans = 0
        for word in words:
            if judge(s, word):
                ans += 1
        return ans