-
Notifications
You must be signed in to change notification settings - Fork 91
/
Subsequence.java
40 lines (34 loc) · 1006 Bytes
/
Subsequence.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
/**
*
*/
import java.util.*;
public class Subsequence {
public List<String> subsequences(String target, String[] dict) {
List<String> res = new ArrayList<>();
for (String word: dict) {
if (target.length() <= word.length() && isSubsequence(target, word)) {
res.add(word);
}
}
return res;
}
public boolean isSubsequence(String s, String t) {
int i = 0;
int j = 0;
char[] chars = s.toCharArray();
char[] chart = t.toCharArray();
while (i < chars.length && j < chart.length) {
if (chars[i] == chart[j]) {
i++;
j++;
} else {
j++;
}
}
return i == chars.length;
}
public static void main(String[] args) {
Insertables ins = new Insertables();
System.out.println(ins.insertables("google", new String[]{"goooooogle", "ddgoogle", "abcd", "googles"}));
}
}