Making a Meal Codechef Solution

Making a Meal Codechef Solution: Today, Chef decided to cook some delicious meals from the ingredients in his kitchen. There are NN ingredients, represented by strings S1,S2,…,SNS1,S2,…,SN. Chef took all the ingredients, put them into a cauldron and mixed them up.

In the cauldron, the letters of the strings representing the ingredients completely mixed, so each letter appears in the cauldron as many times as it appeared in all the strings in total; now, any number of times, Chef can take one letter out of the cauldron (if this letter appears in the cauldron multiple times, it can be taken out that many times) and use it in a meal. A complete meal is the string “codechef”. Help Chef find the maximum number of complete meals he can make!

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • NN lines follow. For each ii (1≤i≤N1≤i≤N), the ii-th of these lines contains a single string SiSi.

Output

For each test case, print a single line containing one integer — the maximum number of complete meals Chef can create.

Constraints

  • 1≤T≤1001≤T≤100
  • 1≤N≤1001≤N≤100
  • |S1|+|S2|+…+|SN|≤1,000|S1|+|S2|+…+|SN|≤1,000
  • each string contains only lowercase English letters

Sample Input 1 

3
6
cplusplus
oscar
deck
fee
hat
near
5
code
hacker
chef
chaby
dumbofe
5
codechef
chefcode
fehcedoc
cceeohfd
codechef

Sample Output 1 

1
2
5

Explanation

Example case 1: After mixing, the cauldron contains the letter ‘c’ 3 times, the letter ‘e’ 4 times, and each of the letters ‘o’, ‘d’, ‘h’ and ‘f’ once. Clearly, this is only enough for one “codechef” meal.

Example case 2: After mixing, the cauldron contains the letter ‘c’ 4 times, ‘o’ 2 times, ‘d’ 2 times, ‘e’ 4 times, ‘h’ 3 times and ‘f’ 2 times, which is enough to make 2 meals.

Making a Meal    – CodeChef Solution in JAVA

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
	{
		 Scanner input = new Scanner(System.in);
		int t = input.nextInt();
    	while(t-- >0){
          //int n = Integer.parseInt(input.next());
          int n = input.nextInt();
          int c=0,o=0,d=0,e=0,h=0,f=0;
          for(int in = 0; in <n; in++){
            String S = input.next();
            for(int i = 0; i<S.length(); i++){
              if(S.charAt(i) == 'c'){
                c++;
              }else if(S.charAt(i) == 'o'){
                o++;
              }else if(S.charAt(i) == 'd'){
                d++;
              }else if(S.charAt(i) == 'e'){
                e++;
              }else if(S.charAt(i) == 'h'){
                h++;
              }else if(S.charAt(i) == 'f'){
                f++;
            	}
            }
          }
          int a[]={c/2,e/2,o,d,h,f};
          Arrays.sort(a);
          System.out.println(a[0]);
          }
	}
}

Making a Meal – CodeChef Solution in CPP

#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
void solve(){
    int c,o,d,e,h,f;
    int t,n;
    string s,ans="";
    cin>>t;
    while(t--){
        ans="";
        cin>>n;
        while(n--){
            cin>>s;
            ans+=s;
        }
        c=0; o=0; d=0; e=0; h=0; f=0;
        for(int i=0;i<ans.size();i++){
                if(ans[i]=='c')      c++;
                else if(ans[i]=='o') o++;       // c/e =    2->1  4->2  6->3  8->4
                else if(ans[i]=='d') d++;     // o/d/h/f=   1->1  2->2  3->3  4->4
                else if(ans[i]=='e') e++;
                else if(ans[i]=='h') h++;
                else if(ans[i]=='f') f++;
            }
            o=min(o,min(d,min(h,f)));
            c=min(c,e);
            cout<<min(c/2,o)<<endl;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);    cin.tie(NULL);
    solve();
    return 0;
}

Making a Meal -CodeChef Solution in Python

def countOfCompleteMeals(S):
    cc, co, cd, ce, ch, cf = 0, 0, 0, 0, 0, 0
    for ingredient in S:
        for c in ingredient:
            if c == 'c':
                cc += 1
            elif c == 'o':
                co += 1
            elif c == 'd':
                cd += 1
            elif c == 'e':
                ce += 1
            elif c == 'h':
                ch += 1
            elif c == 'f':
                cf += 1
    if cc > 1 and co > 0 and cd > 0 and ce > 1 and ch > 0 and cf > 0:
        count = [cc//2, co, cd, ce//2, ch, cf]
        return min(count)
    else:
        return 0
T = int(input())
while T:
    T -= 1
    N = int(input())
    S = []
    for i in range(N):
        S.append(input())
    print(countOfCompleteMeals(S))

Disclaimer: The above Problem(Making a Meal) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment