792. Number of Matching Subsequences

알고리즘 문제풀기 · 2019. 11. 27. 18:13

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

 

 

from collections import defaultdict

class Solution(object):
    def numMatchingSubseq(self, S, words):
        """
        :type S: str
        :type words: List[str]
        :rtype: int
        """
    
        word_dict = defaultdict(list)
        count = 0
        
        for word in words:
            word_dict[word[0]].append(word)    
            
            
        print(word_dict,"WORD_DICT")
        count = 0
        for char in S:
            
            next_list = word_dict[char]
        
            word_dict[char] = []
            
            for word in next_list:
                
                if len(word) == 1:
                    count += 1
                    
                else:
                    word_dict[word[1]].append(word[1:])
        return count
                    
        
    
        

 

'알고리즘 문제풀기' 카테고리의 다른 글

1048. Longest String Chain  (0) 2019.12.05
1219. Path with Maximum Gold  (0) 2019.12.04
988. Smallest String Starting From Leaf  (0) 2019.11.28
392. Is Subsequence  (0) 2019.11.27
946. Validate Stack Sequences  (0) 2019.11.26