-
Notifications
You must be signed in to change notification settings - Fork 5
/
NumberOfMatchingSubsequences.java
44 lines (42 loc) · 1.25 KB
/
NumberOfMatchingSubsequences.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
/*https://leetcode.com/problems/number-of-matching-subsequences/*/
class Solution {
public int numMatchingSubseq(String s, String[] words) {
HashMap<Character,TreeSet<Integer>> map = new HashMap<Character,TreeSet<Integer>>();
int i, n = s.length();
char ch;
for (i = 0; i < n; ++i)
{
ch = s.charAt(i);
if (!map.containsKey(ch)) map.put(ch,new TreeSet<Integer>());
map.get(ch).add(i);
}
int count = 0;
Integer index;
boolean flag;
for (String word : words)
{
ch = word.charAt(0);
if (map.get(ch) == null) continue;
index = map.get(ch).first();
if (index == null) continue;
flag = true;
for (i = 1; i < word.length(); ++i)
{
ch = word.charAt(i);
if (map.get(ch) == null)
{
flag = false;
break;
}
index = map.get(ch).higher(index);
if (index == null)
{
flag = false;
break;
}
}
if (flag) ++count;
}
return count;
}
}