Help me in solving NAME1 problem; I am not able to understand why my solution is incorrect?

Problem Statement Link :- NAME1

/* Approach :- I stored both parent names character count in a map (mp) and then while iterating through their child strings I am storing the character count of their children in a separate map (mpChild) and then at each point checking that whether the character count of a particular character in mpChild is exceeding the character count of that particular character in mp and if its exceeding the final output will be β€œNO” otherwise at end the final output will be β€œYES”. */

Code :-

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

void solve() {

string a, b;
cin >> a >> b;
map<char,int> mp, mpChild;

for(int i=0; i<a.size(); i++){
    mp[a[i]]++;
}

for(int i=0; i<b.size(); i++){
    mp[b[i]]++;
}

int n;
cin >> n;

string name;

for(int i=0; i<n; i++){
    cin >> name;
    for(int j=0; j<name.size(); j++){
        mpChild[name[j]]++;
        if (mpChild[name[j]] > mp[name[j]]){
            cout << "NO" << endl;
            return;
        }
    }
}

cout << "YES" << endl;

}

int main() {

int t;
cin >> t;

while(t--) {
    solve();
}

return 0;

}

@mehul369
U are making mistake like when u are taking the inputs of name strings then when u will get the NO it will return the program and ignores rest inputs .
That is why u r getting wrong answer.

1 Like

@dpcoder_007
Thankyou very very much for pointing out the mistake. I was really commiting a silly blunder.