 # Piece of cake

What is the logic behind Piece of cake problem?

Just find the character which has occurred most in the string. Suppose it has occurred n times. So the remaining characters will be m = size of the string - n. If n = m, then answer will be yes, if not, no.

1 Like

I assume you are new to coding, hence i will give a code with explanation. Else, sudip has already answered it well enough.

What you have to do, is something like this-

``````while(t--)
{
string s;
cin>>s;
int i;
int arr;
for(i=0;i<26;i++)arr[i]=0; //Initialise array.
for(i=0;i<s.length();i++)
{
arr[s[i]-'a']++;//Store the count of letters. Note that since letters range from a to z, letter ch - 'a' is <26.
}
int flag=0;//Will be 0 if false, 1 if true. We assume it to be false by default.
for(i=0;i<26;i++)
{
if(s.length()-arr[i]==arr[i])//If length of string= Occurance of char ch+occurance of other char
{

flag=1;//The condition holds true. Flag set to 1.
break;
}
}
if(flag==1)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}``````
//