题目:给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:如果有大量输入的 S,称作 S1, S2, … , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
示例 1:
输入:s = "abc", t = "ahbgdc"
输出:true
示例 2:
输入:s = "axc", t = "ahbgdc"
输出:false
提示:
0 <= s.length <= 100
0 <= t.length <= 10^4
两个字符串都只由小写字符组成。
方法一:
class Solution {
public boolean isSubsequence(String s, String t) {
int[] index = new int[s.length()];
if(s.length() == 0) {
return true;
}
for(int i = 0; i < s.length(); i++) {
if(t.indexOf(s.charAt(i)) != -1) {
if(i > 0) {
int temp = index[i - 1];
index[i] = t.indexOf(s.charAt(i), temp + 1);
} else {
index[i] = t.indexOf(s.charAt(i));
}
} else {
return false;
}
}
for(int j = 1; j < index.length; j++) {
if(index[j] <= index[j - 1]) {
return false;
}
}
return true;
}
}
方法二:
class Solution {
public boolean isSubsequence(String s, String t) {
int si = 0;
int ti = 0;
while(si < s.length() && ti < t.length()) {
if(s.charAt(si) == t.charAt(ti)) {
si++;
}
ti++;
}
return si == s.length();
}
}