Manna's First Name

Manna is extremely disappointed to find out that no one in his college knows his first name. Even his classmates call him only by his last name. Frustrated, he decides to make his fellow college students know his first name by forcing them to solve this question.

Your task is to find out the number of times SUVO and SUVOJIT appears in the string

Example:

Input:  s = "$$$$$SUVOSUVOJIT$$$$$"
Output: SUVO = 1, SUVOJIT = 1

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void manasFirstName(string s)
{

    int n = s.size();
    int cnt1 = 0cnt2 = 0;
    string str = "SUVO"str1 = "SUVOJIT";

    for (int i = 0i <= n - 4i++)
    {
        int flag = 0;
        if (n - i >= 7)
        {
            for (int j = 0j < 7j++)
            {
                if (s[i + j] != str1[j])
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                cnt2++;
            }
            else if (flag == 1)
            {
                int flag = 0;
                for (int j = 0j < 4j++)
                {
                    if (s[i + j] != str[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                {
                    cnt1++;
                }
            }
        }
        else
        {
            int flag = 0;
            for (int j = 0j < 4j++)
                if (s[i + j] != str[j])
                {
                    flag = 1;
                    break;
                }
            if (flag == 0)
            {
                cnt1++;
            }
        }
    }
    cout << "SUVO = " << cnt1 << ", "
         << "SUVOJIT = " << cnt2 << "\n";
}
int main()
{
    string s = "$$$$$SUVOSUVOJIT$$$$$";

    manasFirstName(s);

    return 0;
}


No comments:

Post a Comment