-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (35 loc) · 1.11 KB
/
Solution.java
File metadata and controls
49 lines (35 loc) · 1.11 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
package leetcode.isSubsequence;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public boolean isSubsequence(String s, String t) {
if(s == null || s.isEmpty()){
return true;
}
Map<Integer,Character> tableChars = new HashMap<>();
for(int i = 0; i < s.length(); i++){
tableChars.put( i, s.charAt(i));
}
StringBuilder sequence = new StringBuilder();
int l = 0;
for(int i = 0; i < t.length(); i++){
char character = t.charAt(i);
char characterNow = tableChars.get(l);
if( character == characterNow){
if(l <= s.length()){
sequence.append(character);
if( l < s.length()-1){
l++;
}
}
}
}
return s.contentEquals(sequence);
}
public static void main(String[] args) {
Solution s = new Solution();
String nums1 = "b";
String nums2 = "abc";
System.out.println(s.isSubsequence(nums1, nums2));
}
}