题目意思:
有两个字符串 s 和 t,t 比较长,要求从 t 删掉点字符能和 s 对等,如果ok的话返回true,不OK返回fasle
贪心思路:
因为是简单的级别,所以按照简单的思路去想,两个数组同时遍历,如果两个字符都相等,两个下标都后移,如果不等,t 的下标后移,s 的下标不动。
最后根据能否遍历完 s 字符串作为判断条件
class Solution {
public:
bool isSubsequence(string s, string t) {
//因为t比s长,遍历t,用个标记及s的下标
int sIndex = 0;
//因为相对位置不变所以直接两个一起遍历就好了
for(int i = 0; i < t.length(); i++){
if(sIndex == s.length()) return true;
//如果等于s的下标往前移
if(s[sIndex] == t[i]){
sIndex += 1;
}
}
if(sIndex == s.length()) return true;
else return false;
}
};