파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 30. 23:45

https://leetcode.com/problems/word-subsets/

 

Word Subsets - 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

n이 그렇게 크지 않아서 무식하게 두 번 loop 돌리면 됨.

class Solution:
    def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
        def count(word):
            ans = [0 for _ in range(26)]
            for l in word:
                ans[ord(l) - ord('a')] += 1
            return ans
        
        ar = [0 for _ in range(26)]
        for w2 in words2:
            for i, c in enumerate(count(w2)):
                ar[i] = max(ar[i], c)
                
        ans = []
        for a in words1:
            if all(x >= y for x, y in zip(count(a), ar)):
                ans.append(a)
        return ans
반응형

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

Today's Challenge  (0) 2022.08.01
Today's Challenge  (0) 2022.07.31
Today's Challenge  (0) 2022.07.29
Today's Challenge  (0) 2022.07.28
Today's Challenge  (0) 2022.07.27
Comments