RBOW - Editorial

PROBLEM LINK:

Practice
Contest

Author: Pankaj Mandrawal
Testers: Akash Kumar Singhal, Prasanna Kumar
Editorialist: Jatin Khandual

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic, STL(Maps, Sets, etc.)

PROBLEM:

You are given a string of length N, consisting of uppercase English letters, each denoting some color (eg: C for Cyan, S for someRandomColor, etc.). Print YES if the string has at least all the colors of the rainbow (VIBGYOR), NO otherwise.

QUICK EXPLANATION:

Maintain a map to store the character frequencies in the string and then test whether the frequencies of the characters in VIBGYOR is at least 1.

EXPLANATION:

Since the characters in VIBGYOR are all distinct, we can map to their letters some value(s) that we can use later to identify whether the string adheres to the given constraints. For this, we’d need an associative container: one that associates some value to some other (the one being “associated with” is called the key and the one being “associated” is called the value). Together, these are called a key-value pair. Here, we can associate the character frequencies to the characters and later, test if each character in VIBGYOR has a frequency of at least 1. If it does, print YES, otherwise NO.

ALTERNATE EXPLANATION:

Alternatively, a set or an array could be used in place of a map, with slight implementation-specific modifications.

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;

string chk = "VIBGYOR";

void testcase() {
    int n; cin >> n;
    string s; cin >> s;
    bool flag = 0;
    map< char, int > mp;
    for (int i = 0; i < n; ++i) {
        mp[s[i]]++;        
    }
    for (char& c : chk) {
        if (mp[c] < 1) {
            flag = 1;
            break;
        }
    }
    cout << (flag ? "NO\n" : "YES\n");
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t; cin >> t;
    while (t--) {
        testcase();
    }
    return 0;
}
Tester's Solution
n = int(input())
for i in range(n):
    k = int(input())
    flag = True
    dic = {};
    for i in ['V','I','B','G','Y','O','R']:
        dic[i] = False
    string = input()
    for i in string:
        if i in ['V','I','B','G','Y','O','R']:
            dic[i] = True
    for y in dic.values():
        if y==False:
            flag = False
            break
    if(flag):
        print("YES")
    else:
        print("NO") 
Tester's Solution II
#include<bits/stdc++.h>
using namespace std;
 
#define __AcHiLlEs ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define i_64 long long
 
void solve() {
    int n, V(1), I(1), B(1), G(1), Y(1), O(1), R(1); cin >> n;
    assert(n >= 1 and n <= 1000);
    string s; cin >> s;
    std::map<char, bool> m;
    for (char &i: s) {
        int x = i - 'A';
        assert(x >= 0 and x <= 25);
        m[i] = 1;
    }
    if (V == m['V'] and I == m['I'] and B == m['B'] and G == m['G'] and Y == m['Y'] and O == m['O'] and R == m['R']) {
        cout << "YES\n";
    }
    else {
        cout << "NO\n";
    }
}
 
signed main() {
    __AcHiLlEs
    i_64 t(1); cin >> t;
    assert(t >= 1 and t <= 1000);
    for (i_64 i = 1; i <= t; solve(), i++);
    return 0;
} 
2 Likes