- leet_code:链接
- 问题描述:判断序列a是否为序列b的子序列
- 输入输出样例:
- intput :a = “abc”, b = “ahbgdc”
- output: true(1).
- c++ 代码实现
class Solution
{
public:
bool isSubsequence(string S1, string S2)
{
int idx = 0, end = S1.size();
for (char val : S2)
{
if (S1[idx] == val)
idx += 1;
if (idx == end)
return true;
}
return false;
}
};
主调函数:
#include<iostream>
using namespace std;
#include<string>
int main(int argc, char* argv[])
{
string S1 = "abc", S2 = "ahbgdc";
Solution solution;
cout << solution.isSubsequence(S1, S2) << endl;
system("pause");
return 0;
}