forked from afrozchakure/Competitive-Programming-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSubsequence.java
More file actions
40 lines (36 loc) · 944 Bytes
/
Copy pathIsSubsequence.java
File metadata and controls
40 lines (36 loc) · 944 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
37
38
39
public class IsSubsequence {
public boolean isSubsequence(String s, String t) {
if (s.isEmpty())
return true;
if (t.isEmpty())
return false;
int i = 0; // s pointer
int j = 0; // t pointer
while (i != s.length() && j != t.length()) {
if (s.charAt(i) == t.charAt(j)) {
i++;
}
j++;
}
if (i == s.length())
return true;
return false;
}
public boolean isSubsequence_II(String s, String t) {
if (s.length() == 0)
return true;
int i = 0, j = 0;
while (j < t.length()) {
char a = s.charAt(i);
char b = t.charAt(j);
if (a == b) {
i++;
if (i == s.length()) {
return true;
}
}
j++;
}
return false;
}
}