파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 8. 13. 23:45

https://leetcode.com/problems/substring-with-concatenation-of-all-words/

 

Substring with Concatenation of All Words - 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

해시 형태를 사용해도 되고 슬라이딩 윈도우를 사용해서 풀어도 되는 문제라고 한다.
솔루션을 봐도 이해가 잘 안감 =_=

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        m = len(words)
        k = len(words[0])
        res = []
        
        for i in range(k):
            left = i
            d = Counter(words)
            
            for j in range(left, len(s) + 1 - k, k):
                word = s[j: j+k]
                d[word] -= 1
                
                while d[word] < 0:
                    d[s[left: left + k]] += 1
                    left += k
                
                if left + k * m == j + k:
                    res.append(left)
        
        return res
반응형

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

Today's Challenge  (0) 2022.08.15
Today's Challenge  (0) 2022.08.14
Today's Challenge  (0) 2022.08.12
Today's Challenge  (0) 2022.08.11
Today's Challenge  (0) 2022.08.10
Comments