Skip to content

Commit d6a996d

Browse files
author
Partho Biswas
committed
792. Number of Matching Subsequences
1 parent 897d950 commit d6a996d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ I have solved quite a number of problems from several topics. See the below tabl
124124
|19| [904. Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/solution/)| [Python](leetcode.com/python/904_Fruit_Into_Baskets.py)| [Article](https://www.educative.io/courses/grokking-the-coding-interview/Bn2KLlOR0lQ), [Video 1](https://www.youtube.com/watch?v=s_zu2dOkq80), [Video 2](https://www.youtube.com/watch?v=za2YuucS0tw)|
125125
|20| [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Python](leetcode.com/python/56_Merge_Intervals.py)| [Article](https://leetcode.com/problems/merge-intervals/discuss/21227/7-lines-easy-Python)|
126126
|21| [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)| [Python](leetcode.com/python/334_Increasing_Triplet_Subsequence.py)| --- | Medium | --- |
127+
|22| [792. Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/)| [Python](leetcode.com/python/792_Number_of_Matching_Subsequences.py)| **[Official](https://leetcode.com/problems/number-of-matching-subsequences/discuss/117634/Efficient-and-simple-go-through-words-in-parallel-with-explanation/)** | Medium | **Very tricky. Check again** |
127128

128129

129130
### [String](https://leetcode.com/explore/learn/card/array-and-string/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import defaultdict
2+
3+
4+
class Solution(object):
5+
6+
def numMatchingSubseq(self, S, words):
7+
waiting = defaultdict(list)
8+
for w in words:
9+
waiting[w[0]].append(iter(w[1:]))
10+
for c in S:
11+
for it in waiting.pop(c, ()):
12+
waiting[next(it, None)].append(it)
13+
return len(waiting[None])
14+
15+
16+
17+
18+
sol = Solution()
19+
S = "abcde"
20+
words = ["a", "bb", "acd", "ace"]
21+
out = sol.numMatchingSubseq(S, words)
22+
print('Res: ', out)

0 commit comments

Comments
 (0)