Skip to main content

792 - Number of Matching Subsequences

Details

KeyValue
Linkhttps://leetcode.com/problems/number-of-matching-subsequences/
LanguagePython 3
Runtime360 ms, faster than 96.34% of Python3 online submissions for Number of Matching Subsequences
Memory Usage15.6 MB, less than 51.10% of Python3 online submissions for Number of Matching Subsequences
DatastructuresList[str]
Algorithms?
ComplexityTime: ? Memory: ?

Procedure

  1. ...

Code

class Solution:
def numMatchingSubseq(self, s: str, words: List[str]) -> int:
word_dict, result = defaultdict(list), 0

for word in words: word_dict[word[0]].append(word)

for char in s:
if char in word_dict:
words_expecting_char = word_dict.pop(char)
for word in words_expecting_char:
if len(word) == 1:
result += 1
else:
word_dict[word[1]].append(word[1:])
return result