-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsSubsequence.java
78 lines (66 loc) · 1.53 KB
/
IsSubsequence.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// https://leetcode.com/problems/is-subsequence/
// #binary-search #dynamic-programming #greedy
class Solution {
// another way: two pointer
// DP
public boolean isSubsequence(String s, String t) {
int len = t.length();
if (len == 0) {
return s.isEmpty();
}
int[][] next = new int[len][26];
// initial
int last_c = t.charAt(len - 1) - 'a';
for (int i = 0; i < 26; i++) {
if (i == last_c) {
next[len - 1][i] = len - 1;
} else {
next[len - 1][i] = -1;
}
}
for (int i = len - 2; i >= 0; i--) {
char c = t.charAt(i);
for (int j = 0; j < 26; j++) {
if (c == 'a' + j) {
next[i][j] = i;
} else {
next[i][j] = next[i + 1][j];
}
}
}
int idx = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (idx >= next.length) {
return false;
}
if (next[idx][c - 'a'] == -1) {
return false;
}
idx = next[idx][c - 'a'] + 1;
}
return true;
}
}
/*
s = "abc", t = "ahbgdc"
s = abc
t = askjdnfgksdfgbksjdnfgksjdngc
O(q * (n + m))
O(q * n + m)
next[i][c]: first character c from index i
=>
index = 0;
for each character c of s:
if (next[index][c] == -1) return false
index = next[index][c];
for i -> n -> 0:
next[i][c]:
c == t[i]:
next[i][t[i]] = i
c != t[i]:
next[i][c] = next[i + 1][c]
for index = 0; index < n; index++
for (i:index -> i < n):
next[index][t[i]] = i;
*/