Chef and Feedback | CodeChef Solution

Hello coders, today we are going to solve Chef and Feedback CodeChef Solution whose Problem Code is ERROR.

Chef and Feedback

Contents

Task

Lots of geeky customers visit our chef’s restaurant everyday. So, when asked to fill the feedback form, these customers represent the feedback using a binary string (i.e a string that contains only characters ‘0’ and ‘1’.Now since chef is not that great in deciphering binary strings, he has decided the following criteria to classify the feedback as Good or Bad :

If the string contains the substring “010” or “101”, then the feedback is Good, else it is Bad. Note that, to be Good it is not necessary to have both of them as substring.

So given some binary strings, you need to output whether according to the chef, the strings are Good or Bad.

Input Format

The first line contains an integer T denoting the number of feedbacks. Each of the next T lines contains a string composed of only ‘0’ and ‘1’.

Output Format

For every test case, print in a single line Good or Bad as per the Chef’s method of classification.

Constraints

  • ≤ T ≤ 100
  • ≤ |S| ≤ 105

Sum of length of all strings in one test file will not exceed 6*106.

Example

Sample Input

2
11111110
10101010101010

Sample Output

Bad
Good

Explanation

Example case 1.
The string doesn’t contain 010 or 101 as substrings.

Example case 2.
The string contains both 010 and 101 as substrings.

Solution – Chef and Feedback

C++

#include <bits/stdc++.h>
using namespace std;
bool isSubstring(string s1, string s2)
{
    int n = s1.size();
    int m = s2.size();
    for (int i = 0; i < n - m + 1; i++)
    {
        int j;
        for (j = 0; j < m; j++)
        {
            if (s1[i + j] != s2[j])
                break;
        }
        if (j == m)
            return true;
    }
    return false;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        if (isSubstring(s, "101") || isSubstring(s, "010"))
        {
            cout << "Good" << endl;
        }
        else
            cout << "Bad" << endl;
    }
    return 0;
}

Python

for i in range(int(input())):
    s = input()
    if "101" in s or "010" in s:
        print("Good")
    else:
        print("Bad")

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        while(t-->0){
            String s = scan.next();
            String a = "101", b = "010";
            if(s.contains(a) || s.contains(b)){
                System.out.println("Good");
            }
            else{
                System.out.println("Bad");
            }
        }
	}
}

Disclaimer: The above Problem (Chef and Feedback) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.