A Basic Fundamental Concept

 The Problem "String Game" of CodeChef is interesting if we are trying to solve it using cpp, because we don't have any function to get the frequencies of elements in an iterable unlike in Python and so it depends on the individual programmer what type of approach they are using. Basically in this problem it all boils down to getting the frequencies of all char in the string and making sure that the string can be divided equally to Alice and Bob, and for that every char should occur even no. of times. 

In Python it is fairly simple, but the joy is in solving this problem in Cpp, because the things you get in Cpp are challenge and corner cases which are not there in Python bcz of the functions in it.

There are 2 codes for this problem provided below, the 1st approach passes all the test cases but the 2nd one fails to pass some of them because of time complexity constraints. This failure is because of the approach to find the frequency of the chars, basically in 1st approach I am finding the freq of every char and then checking if it is even or odd and depending on that printing YES or NO statements, the mian thing is that I am not touching a char which has been previously visited by me, that is, I am ignoring the char which have their instances before the current instance. I am not doing this in the 2nd approach, instead I am finding the freq for all chars not caring if I have visited that char or not. Which increases my TC and hence is not optimal and shows what we shouldn't do, and most importantly we wouldn't have learnt this if we would have used the fairly easy functions of Python.

The 1st Approach:-

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int T,n;
    string s;
    cin>>T;
    while(T>0){
        cin>>n>>s;
        int done=0,check=0;
        for(int i=0;i<n;i++){
            int count=1,chk=0;
            if(i!=0){
                // Will check if this char has been checked before or not
                for(int j=i-1;j>=0;j--){
                    if(s[j]==s[i]){
                        // Means the char was checked beforehand only
                        chk=1;
                        break;
                    }
                }
                if(chk==1){
                    continue;
                }
            }
            if(i!=n-1){
                for(int k=i+1;k<n;k++){
                    if(s[k]==s[i]){
                        count++;
                    }
                }       
            }
            else{
                // The char has came for the first time and is the last elem 
                // in the string
                cout<<"NO"<<endl;
                done=1;
                break;
            }
            // Now we have the frequency of the curr elem at this point
            if(count%2!=0){
                done=1;
                cout<<"NO"<<endl;
                break;
            }
            
        }
        if(done!=1){
            cout<<"YES"<<endl;
        }
        
        T--;
    }
    return 0;
}

The 2nd Approach:-

#include <iostream>
using namespace std;
/** Attempt No.-2 String Game- CPP Version **/
/** Time Limit Exceeded **/
/*
 * The Problem with this code is that for every char it is getting it's freq
 * even if the char has been seen before and it's freq is checked which is 
 * not needed
 * and this is why it is getting TLE because this is not needed for the chars 
 * which have been visited once.
 *
 */
int main() {
    int T,n;
    // cout<<"What's T? ";
    cin>>T;
    // cout<<endl;
    while(T>0){
        // cout<<"What's n? ";
        cin>>n;
        // cout<<endl;
        int chk=0;
        string s;
        // cout<<"What's s? ";
        cin>>s;
        // cout<<endl;
        if(n%2!=0){
            cout<<"NO"<<endl;
            T--;
            continue;
        }
        for(int i=0;i<n;i++){
            int freq=1;
            if(i>0){
                for(int j=i-1;j>=0;j--){
                    if(s[j]==s[i]){
                        freq+=1;
                    }
                }    
            }
            if(i!=n-1){
                for(int k=i+1;k<n;k++){
                    if(s[k]==s[i]){
                        freq++;   
                    }
                }    
            }
            if(freq%2!=0){
                cout<<"NO"<<endl;
                chk=1;
                break;
            }
        }
        if(chk==1){
            T--;
            continue;
        }
        cout<<"YES"<<endl;
        T--;
    }
    return 0;
}

Comments

Popular Posts