LeetCode/NeetCode

567. Permutation in String

hyunkookim 2025. 4. 16. 04:58

567. Permutation in String

from collections import defaultdict

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        def getCountDictString(strs):
            cm = defaultdict(int)
            for s in strs:
                cm[s] += 1
            cm = dict(sorted(cm.items()))
            res = ""
            for k, v in cm.items():
                res += f"{k}_{v},"
            return res

        left = 0
        for right in range(len(s2)):
            if right - left + 1 == len(s1):
                # ✅ 마지막 문자까지 포함해야 하므로 right+1
                if getCountDictString(s2[left:right+1]) == getCountDictString(s1):
                    return True
                left += 1

        return False

 

https://youtu.be/UbyhOgBN834