Skip to content

Commit 8b2f2b1

Browse files
committed
[NEW] #792 Number of Matching Subsequences
1 parent 0f1dcd7 commit 8b2f2b1

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Time spent : 20 min(See hint)
2+
3+
GIVEN: string S and a dictionary of words
4+
5+
RETURNS: the number of words[i] that is a subsequence of S
6+
7+
```
8+
Example :
9+
Input:
10+
S = "abcde"
11+
words = ["a", "bb", "acd", "ace"]
12+
Output: 3
13+
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".
14+
```
15+
16+
Algorithm:
17+
18+
Put words into buckets by starting character. For words above
19+
20+
we have head[0] : "a", "acd", "ace", head[1] : "bb"
21+
22+
For each word, we create a class node, we have an index indicate the index of current node character, and the string word.
23+
24+
1st:
25+
26+
"a", index = 1 == "a".length(), ans++
27+
28+
"acd", index = 1 != "acd".length(), add "acd" to head[c - '0']
29+
30+
"ace", index = 1 != "ace".length(), add "ace" to head[c - '0']
31+
32+
2nd:
33+
34+
"b", index = 1 != "bb".length(), add "bb" to head[b - '0']
35+
36+
3rd:
37+
38+
...
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int numMatchingSubseq(String S, String[] words) {
3+
int result = 0;
4+
List<Node>[] heads = new ArrayList[26];
5+
for (int i = 0; i < 26; i++) {
6+
heads[i] = new ArrayList<>();
7+
}
8+
9+
for (String word : words) {
10+
heads[word.charAt(0) - 'a'].add(new Node(word, 0));
11+
}
12+
13+
for (char c : S.toCharArray()) {
14+
List<Node> old_bucket = heads[c - 'a'];
15+
heads[c - 'a'] = new ArrayList<>();
16+
for (Node node : old_bucket) {
17+
node.index++;
18+
if (node.index == node.word.length()) {
19+
result++;
20+
}
21+
else {
22+
heads[node.word.charAt(node.index) - 'a'].add(node);
23+
}
24+
}
25+
old_bucket.clear();
26+
}
27+
return result;
28+
}
29+
}
30+
31+
class Node {
32+
String word;
33+
int index;
34+
public Node(String word, int index) {
35+
this.word = word;
36+
this.index = index;
37+
}
38+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
| 397 | 859 | [Buddy Strings][859] | Java | Easy | 2018.6.24 | String | |
402402
| 398 | 856 | [Score of Parentheses][856] | Java | Medium | 2018.6.24 | DFS | |
403403
| 399 | 825 | [Friends Of Appropriate Ages][825] | Java | Medium | 2018.6.25 | Array | |
404+
| 400 | 792 | [Number of Matching Subsequences][791] | Java | Medium | 2018.6.26 | Array | |
404405

405406

406407
[1]:<https://leetcode.com/problems/two-sum/> "Two Sum"
@@ -785,6 +786,7 @@
785786
[781]:<https://leetcode.com/problems/rabbits-in-forest/> "Rabbits in Forest"
786787
[783]:<https://leetcode.com/problems/minimum-distance-between-bst-nodes/> "Minimum Distance between BST nodes"
787788
[784]:<https://leetcode.com/problems/letter-case-permutation/> "Letter Case Permutation"
789+
[792]:<https://leetcode.com/problems/number-of-matching-subsequences/> "Number of Matching Subsequences"
788790
[796]:<https://leetcode.com/problems/rotate-string/> "Rotate String"
789791
[801]:<https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/> "Minimum Swaps to Makde Sequences Increasing"
790792
[813]:<https://leetcode.com/problems/largest-sum-of-averages/> "Largest Sum of Averages"

0 commit comments

Comments
 (0)