-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode792.java
More file actions
47 lines (44 loc) · 1.44 KB
/
Copy pathleetcode792.java
File metadata and controls
47 lines (44 loc) · 1.44 KB
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 leetcode;
import java.util.*;
/*
* 1.贪心法简单粗暴
* 2.二分法 : 其实还是离不开贪心法
* 1.先对word 去重
* 2.记录S 中么一个元素出现的位置,维护一个map<String,List<Integer>> List<Integer>是相对有序的
* 遍历每一个word 然后遍历word 中的每一个字符在map中的是不是满足位置上的约束,都满足就加一
* 3. 字典树 https://blog.csdn.net/jiutianhe/article/details/8076835
*
*
* */
public class leetcode792 {
public static void main(String[] args) {
System.out.println(
new Solution().numMatchingSubseq(
"abcde",new String[]{
"a", "bb", "acd", "ace"
}
)
);
}
}
class Solution {
public int numMatchingSubseq(String S, String[] words) {
Map<String,Integer> map=new HashMap<String,Integer>();
int result=0;
for(int i=0;i<words.length;i++){
if(map.containsKey(words[i])){
map.put(words[i],map.get(words[i])+1);
}else{
map.put(words[i],1);
}
}
List<String> set= new LinkedList<>(map.keySet());
for(Iterator iterator = set.iterator(); iterator.hasNext();){
String key=(String) iterator.next();
if(leetcode392.isSubsequence(key,S)){
result+=map.get(key);
}
}
return result;
}
}