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:-
The 2nd Approach:-
Comments
Post a Comment