CGPHY001 - Editorial

PROBLEM LINK:

Contest: CGRP2021

Setter: Harsh Prajapati
Tester: Harsh Prajapati
Editorialist: Harsh Prajapati

DIFFICULTY:

Easy

PREREQUISITES:

String

PROBLEM:

Given a string S, S is an acceptable string if it has the characters - S H A Z A M ! , in the correct order. Your program has to determine whether a given string is acceptable or not.

EXPLANATION:

We will first create a list li containing the characters - S H A Z A M !

Now we create two counters x and counter. x will indicate the position of the character we are currently checking in li and counter will indicate the number of characters found.

Next, we will begin traversing the string S and check each character for the character at x in li, if they match we update both the counters, so that going forward we check the next character in li.

When we have traversed the whole string, we will check counter, if it is 7 (Indicating all the characters have been found), we print YES, otherwise we print NO.

SOLUTIONS:

Setter's Solution (Python3)
t = int(input())
while t > 0:
    S = input()
    li = "SHAZAM!"
    count = 0
    x = 0
    for i in S:
        if i == li[x]:
            count += 1
            x += 1
    if count == 7:
        print("YES")
    else:
        print("NO")
    t -= 1
Setter's Solution (C++)
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;cin.ignore();
    string arr[n+1];
 
    char shz[] = {'S', 'H', 'A', 'Z', 'A', 'M', '!'};
    for (int i = 0; i < n; i++)
    {
       getline(cin>>ws,arr[i]);
    }
    // for (int i = 0; i < n; i++)
    // {cout<<arr[i]<<endl;}
    for (int i = 0; i < n; i++)
    {
        string s = arr[i];
        // cout<<arr[i]<<endl;
        int x = 0, count = 0;
        for (int j = 0; j < s.length(); j++)
        {
            if (shz[x] == s[j])
              { count++;x++;}
        }
        if (count == 7)
        {
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}