forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_792.java
47 lines (43 loc) · 1.52 KB
/
_792.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _792 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/number-of-matching-subsequences/discuss/1290406/C%2B%2BJavaPython-Next-Letter-Pointers-Picture-explain-O(N-%2B-S)
*/
public int numMatchingSubseq(String s, String[] words) {
List<Node>[] buckets = new ArrayList[26];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<>();
}
for (String word : words) {
char start = word.charAt(0);
buckets[start - 'a'].add(new Node(word, 0));
}
int result = 0;
for (char c : s.toCharArray()) {
List<Node> currBucket = buckets[c - 'a'];
buckets[c - 'a'] = new ArrayList<>();
for (Node node : currBucket) {
node.index++;
if (node.index == node.word.length()) {
result++;
} else {
char start = node.word.charAt(node.index);
buckets[start - 'a'].add(node);
}
}
}
return result;
}
private class Node {
String word;
int index;
public Node(String word, int index) {
this.word = word;
this.index = index;
}
}
}
}