-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution.java
More file actions
57 lines (46 loc) · 1.52 KB
/
Solution.java
File metadata and controls
57 lines (46 loc) · 1.52 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
48
49
50
51
52
53
54
55
56
57
/*
* Author: Minho Kim (ISKU)
* Date: November 8, 2019
* E-mail: minho.kim093@gmail.com
*
* https://github.com/ISKU/Algorithm
* https://leetcode.com/problems/number-of-matching-subsequences/
*/
class Solution {
public int numMatchingSubseq(String S, String[] words) {
List<Integer>[] letters = new ArrayList[26];
for (int i = 0; i < 26; i++)
letters[i] = new ArrayList<Integer>() {{ add(-1); }};
for (int i = 0; i < S.length(); i++)
letters[S.charAt(i) - 'a'].add(i);
int answer = 0;
for (String word : words) {
boolean contains = true;
int index = 0;
for (int i = 0; i < word.length(); i++) {
List<Integer> indexes = letters[word.charAt(i) - 'a'];
int next = lowerBound(indexes, index);
if (next == -1) {
contains = false;
break;
}
index = indexes.get(next) + 1;
}
if (contains)
answer++;
}
return answer;
}
private int lowerBound(List<Integer> array, int target) {
int l = 0;
int r = array.size() - 1;
while (l < r) {
int mid = (l + r) / 2;
if (array.get(mid) < target)
l = mid + 1;
else
r = mid;
}
return array.get(l) < target ? -1 : l;
}
}