파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 20. 23:45

https://leetcode.com/problems/number-of-matching-subsequences/

 

Number of Matching Subsequences - 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 numMatchingSubseq(self, s: str, words: List[str]) -> int:
        ln = len(words)
        dct = defaultdict(list)
        wordLvls = [0]*ln
        
        for i, word in enumerate(words):
            dct[word[0]].append(i)
         
        ans = 0
        for c in s:
            if c not in dct:
                continue
            lst = dct.pop(c)
            for i in lst:
                wordLvls[i] += 1
                if wordLvls[i] < len(words[i]):
                    dct[words[i][wordLvls[i]]].append(i)
                else:
                    ans += 1                        
        return ans
반응형

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

Today's Challenge  (0) 2022.07.22
Today's Challenge  (0) 2022.07.21
Today's Challenge  (0) 2022.07.19
Today's Challenge  (0) 2022.07.18
Today's Challenge  (0) 2022.07.17
Comments