-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIs_Subsequence_392.java
More file actions
36 lines (32 loc) · 894 Bytes
/
Copy pathIs_Subsequence_392.java
File metadata and controls
36 lines (32 loc) · 894 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
package leetcode;
import java.util.*;
/**
* Created by bosun on 5/3/17.
*/
public class Is_Subsequence_392 {
public boolean isSubsequence(String s, String t) {
List<Integer>[] idxList = new List[26];
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
if (idxList[c - 'a'] == null) idxList[c - 'a'] = new ArrayList<>();
idxList[c - 'a'].add(i);
}
int tIdx = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (idxList[c - 'a'] == null) {
return false;
}
int j = Collections.binarySearch(idxList[c - 'a'], tIdx);
if (j < 0) j = - (j + 1);
if (j >= idxList[c - 'a'].size()) {
return false;
}
tIdx = idxList[c - 'a'].get(j) + 1;
}
return true;
}
public static void main(String[] args) {
new Is_Subsequence_392().isSubsequence("ab", "abc");
}
}