Search Me

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<stringint>> v;
    while (i < n)
    {
        string str = "";
        int j = i;
        while (i < n && s[i] != ' ')
        {
            str += s[i];
            i++;
        }
        i++;
        v.push_back({strj});
    }
    int flag = 0;
    for (int i = 0i < v.size() - 1i++)
    {
        if (v[i].first == "to" && v[i + 1].first == "the")
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        return false;
    else
        return true;
}
int main()
{
    string s = "to order the monk";

    if (searhMe(s))
        cout << "YES\n";
    else
        cout << "NO\n";
    return 0;
}


No comments:

Post a Comment