-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumMatchingSubseq.java
More file actions
40 lines (32 loc) · 872 Bytes
/
NumMatchingSubseq.java
File metadata and controls
40 lines (32 loc) · 872 Bytes
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
import java.util.HashMap;
/**
* 792. 匹配子序列的单词数
*/
public class NumMatchingSubseq {
public int numMatchingSubseq(String s, String[] words) {
int res = 0;
// 去重
HashMap<String, Integer> map = new HashMap<>();
for(String word: words){
map.put(word, map.getOrDefault(word, 0)+1);
}
for(String word: map.keySet()){
if(isMatch(s, word)) {
res += map.get(word);
}
}
return res;
}
boolean isMatch(String s, String word){
int left=0, right=0;
while (left<s.length() && right<word.length()){
if(s.charAt(left)==word.charAt(right)){
left++;
right++;
}else{
left++;
}
}
return right==word.length();
}
}