Getting wrong answer for chef and string problem in beginners section

#include
#include
#include<string.h>
using namespace std;
int main()
{
int j=0,count=0,a=0,i;
string s;
char chef[]={‘C’,‘H’,‘E’,‘F’},arr[2000];
cin>>s;
for(i=0;i<s.size();i++)
{
if(s[i]==‘C’||s[i]==‘H’||s[i]==‘E’||s[i]==‘F’)
{

        if(s[i]==chef[j])  
        {
            arr[a]=chef[j];  
            a++;  
            j++;  
            if(arr[a-1]=='F')  
            {  
                count+=1;  
                j=0;  
            }  
        }  
    }  
}  
    cout<<count;  
    return 0;  
}

Whenever you ask a doubt about a problem, always provide the problem link.

4 Likes

Your code prints wrong answer for following test case-

Input 
CCHHEEFF
Correct Output
2
Your Output
1

The correct procedure is in counting the minimum amount of ‘CHEF’ upto index i (which is minimum amount of ‘c’, ‘h’ ‘e’ and 'f in order)

My code for reference-

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
 
int main() {
	// your code goes here
	string s;
	cin>>s;
	int n=s.length(),i,j=0,k;
	int arr[4]={0},cE[n],cF[n],cH[n];
	for(i=n-1;i>=0;i--)
	{
	    if(s[i]=='C')
	       arr[0]=min(arr[1],arr[0]+1);
	   else if(s[i]=='H')
	      arr[1]=min(arr[2],arr[1]+1);
	   else if(s[i]=='E')
	      arr[2]=min(arr[3],arr[2]+1);
	   else if (s[i]=='F')
	      arr[3]++;
	   	}
	int min=arr[0];
	for(i=1;i<4;i++)
	  if(arr[i]<min)
	    min=arr[i];
	cout<<min<<endl;
	return 0;
}
1 Like

PS: Sorry, copied the code instead of link. Dont have enough courage to go back to the section and look for this problem again…

Please provide problem link whenever you ask a question. Its convenient for us to look over the question then.

Ohh, one more query from beginner section, that too without problem link.pls provide problem link with your question(highly recommended).
Finally, find out the problem link from your profile.
Here is the case where your solution fails.
test case:

INPUT:

CHECHEFCHEFF

OUTPUT:

3

Modify your code according to this kind of test case.
feel free to comment for any further queries.

1 Like

Haha,I am too late :slight_smile:

This is my first time in the forum.I will definitely do that in future,thanks for your input.

1 Like