题目描述

思路
利用一个index/指针来逐个匹配目标字符串 “hello”。从j=0开始,如果找到了j就加一。如果最后j=5的话就说明找到了。
代码
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <string> using namespace std;
int main() { string s; cin >> s;
string target = "hello"; int j = 0;
for (char c : s) { if (c == target[j]) { j++; } if (j == target.size()) { break; } }
if (j == target.size()) { cout << "YES" << endl; } else { cout << "NO" << endl; }
return 0; }
|