-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path392.java
38 lines (38 loc) · 1.34 KB
/
392.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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean isSubsequence(String s, String t) {
char[] ch = s.toCharArray();
int index = -1;
for(char c:ch){
index = t.indexOf(c, index + 1);
if(index == -1) return false;
}
return true;
}
}
__________________________________________________________________________________________________
sample 47848 kb submission
class Solution {
public boolean isSubsequence(String s, String t) {
if(s == null || s.length() == 0) return true;
if(t == null || t.length() == 0){
if(s == null || s.length() == 0) return true;
else return false;
}
Stack<Character> stack = new Stack<>();
for(Character ch : s.toCharArray()){
stack.push(ch);
}
char[] longer = new char[t.length()];
longer = t.toCharArray();
for(int i = longer.length - 1; i >= 0; i--){
// if(stack.isEmpty()) return false;
if(!stack.isEmpty() && longer[i] == stack.peek()){
stack.pop();
}
}
return stack.isEmpty();
}
}
__________________________________________________________________________________________________