Nayan loves to play with strings. He often tests people by challenging them with tough substrings. He has a problem for you as well.
You are given a substring " to the ", your task is to check whether this substring is present in the input string or not. If present print "YES" otherwise print "NO".
Example:
Input: s = "to order the monk"
Output: NO
Approach
C++
#include <bits/stdc++.h>using namespace std;bool searhMe(string s){int n = s.size();int i = 0;vector<pair<string, int>> v;while (i < n){string str = "";int j = i;while (i < n && s[i] != ' '){str += s[i];i++;}i++;v.push_back({str, j});}int flag = 0;for (int i = 0; i < v.size() - 1; i++){if (v[i].first == "to" && v[i + 1].first == "the"){flag = 1;break;}}if (flag == 0)return false;elsereturn true;}int main(){string s = "to order the monk";if (searhMe(s))cout << "YES\n";elsecout << "NO\n";return 0;}
No comments:
Post a Comment