看到这个题我首先想到的方法就是遍历s中的字符,去t中寻找是否存在。
这个方法和贪心算法的关系是什么,请您解惑。
public boolean isSubsequence(String s, String t) {
boolean res = true;
int curIndex = 0;
char[] tArr = t.toCharArray();
for(char cs:s.toCharArray()) {
boolean finded = false;
for(int i=curIndex;i<tArr.length;i++) {
if(tArr[i]==cs) {
finded = true;
curIndex = i + 1;
break;
}
}
if(!finded) {
res = false;
break;
}
}
return res;
}