파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 31. 23:45

https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/

 

Check If a String Contains All Binary Codes of Size K - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

간단하게 풀 수 있을 줄 알았는데, 내가 작성한 코드는 TL 남 ㅠㅠ

class Solution:
    def hasAllCodes_mine(self, s: str, k: int) -> bool:
        kk = 2 ** k
        ss = set()

        for i in range(len(s)):
            for j in range(i, len(s) - k + 1):
                temp = int('0b' + s[i:i+k], 2)
                ss.add(temp)

        ss = sorted(ss)

        for i in range(kk):
            if i != ss[i]:
                return False

        return True

    def hasAllCodes(self, s: str, k: int) -> bool:
        got = {s[i - k:i] for i in range(k, len(s) + 1)}
        return len(got) == 1 << k
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.06.02
Today's Challenge  (0) 2022.06.01
Today's Challenge  (0) 2022.05.30
Today's Challenge  (0) 2022.05.29
Today's Challenge  (0) 2022.05.28
Comments