题目
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"
是"abcde"
的一个子序列,而"aec"
不是)。
示例 1:
输入:s = "abc", t = "ahbgdc" 输出:true
示例 2:
输入:s = "axc", t = "ahbgdc" 输出:false
C++代码
#include <iostream>
using namespace std;
//定义判断子序列函数,双指针解决
bool isSubsequence(string s, string t) {
int i = 0, j = 0;
while (i < s.size() && j < t.size()) {
if (s[i] == t[j]) {
++i;
++j;
}
else {
++j;
}
}
return i == s.size();
}
int main() {
string s = "abc";
string t = "ahbgdc";
bool ans = isSubsequence(s, t);
cout << boolalpha << ans << endl;
return 0;
}
分析
双指针遍历字符串