You are not logged in. Please login at www.codechef.com to post your questions!

×

Piece of cake

What is the logic behind Piece of cake problem?

asked 06 Jul '17, 18:21

sandeep_1097's gravatar image

2★sandeep_1097
1
accept rate: 0%


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.

link

answered 06 Jul '17, 18:51

sudip_95's gravatar image

4★sudip_95
7556
accept rate: 10%

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[26];
        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;
    }
link

answered 06 Jul '17, 19:33

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
accept rate: 18%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×553

question asked: 06 Jul '17, 18:21

question was seen: 572 times

last updated: 06 Jul '17, 19:33