-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSubsequence.java
More file actions
60 lines (56 loc) · 1.51 KB
/
IsSubsequence.java
File metadata and controls
60 lines (56 loc) · 1.51 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
58
59
60
package com.cier.solution.dp;
/**
* @program: Leetcode
* @description: 判断子序列
* @author: liuenci
* @create: 2020-12-18 18:53
**/
public class IsSubsequence {
public static void main(String[] args) {
IsSubsequence subsequence = new IsSubsequence();
System.out.println(subsequence.isSubsequence2("abc", "ahbgdc"));
}
/**
* 给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
* 输入:s = "aa", t = "ba"
*
* @param s
* @param t
* @return
*/
public boolean isSubsequence(String s, String t) {
int index = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
boolean flag = false;
for (int j = index; j < t.length(); j++) {
if (t.charAt(j) == c) {
index = j + 1;
flag = true;
break;
}
}
if (!flag) {
return false;
}
}
return true;
}
public boolean isSubsequence2(String s, String t) {
if (s.length() == 0) {
return true;
}
int lowIndex = 0;
int highIndex = 0;
for (int i = 0; i < t.length(); i++) {
if (lowIndex > s.length() - 1) {
return true;
}
if (s.charAt(lowIndex) == t.charAt(highIndex)) {
lowIndex++;
}
highIndex++;
}
return lowIndex > s.length() - 1;
}
}