MONCRUSH - Editorial

PROBLEM LINK:

Practice

Author: Ayush Shukla
Tester: Sparsh Kedia
Editorialist: Abhishek Kumar

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Strings

PROBLEM:

Here we have to only check for the i_{th} letter of the string in the i_{th} word.

EXPLANATION:

First of all we need to check that for each testcase the value of N is equal to the length of the string S, as the characters have to be taken from consecutive strings, and no strings can be ignored.
Now, we can observe that any charcter from a string can be shifted to the first position by the two rules given. Thus all we need to find is, whether for each character S_i present in the string S, the same character is present in the i_{th} string given to us, so that we can shift it to the first position by swaping.
If this is true for each i from i=0 to i=N-1, then the answer is YES, else NO.

SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>
using namespace std;
#define ll long long
main(){
int t;
cin >> t;
while(t–){
string s;
cin>>s;
ll n;
cin>>n;
if(n!=s.length()){
for(ll i=0;i<n;i++){
string a;
cin>>a;
}
cout<<“NO\n”;
}
else{
ll flag = 1;
for(ll i=0;i<n;i++){
string a;
cin>>a;
ll c = 1;
for(ll j=0;j<a.length();j++)
if(a[j]==s[i])c=0;
if(c) flag = 0;
}
if(flag)cout<<“YES\n”;
else cout<<“NO\n”;
}
}
}

Tester's Solution
import math
t = int(input())
while t>0 : 
    s = input()
    l = len(s)
    n = int(input())
    if(n!=l):
        for i in range(0,n):
            str = input()
        print("NO")
    else:
        flag = 1
        for i in range(0,n):
            str = input()
            c = 1
            for j in range(0,len(str)):
                if(str[j]==s[i]):
                    c=0
            if(c==1):
                flag=0
        
        if(flag==1):
            print("YES")
        else:
            print("NO")
            
    t = t-1 

Feel free to share your approach :smile:. Happy Coding :smile: