Chef And His Characters Codechef Solution

Chef And His Characters Codechef Solution: Chef was reading a book. He decided to highlight all the lovely sentences of the book. He calls a sentence a lovely sentence if, after removing all the non-alphabetical characters (like punctuation and spaces) from it and converting all uppercase letters to lowercase, it is possible to choose four contiguous characters from the sentence and reorder them to create the string “chef”.

Chef has almost completed the highlighting work, only a few sentences are left to be checked and highlighted. Now, he wants your help in checking the remaining sentences, as he has to help his mother in the kitchen.

Each sentence s has already been preprocessed — all non-alphabetical characters have been erased and all remaining uppercase characters converted to lowercase. You should determine whether s is a lovely sentence or not. If it is a lovely sentence, then Chef is also interested in the number of possible ways to select 4 contiguous characters that can be reordered to give his name (the string “chef”).

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains a single string s, denoting a preprocessed sentence.

Output

  • For each test case, print a single line.
  • If the string s is not a lovely string, this line should contain a single string “normal” (without quotes).
  • Otherwise, it should contain the string ”lovely” (without quotes) followed by one integer — the number of ways to select the four contiguous characters.

Constraints

  • 1 ≤ T ≤ 200,000
  • 1 ≤ |s| ≤ 500,000
  • sum of |s| for all test cases ≤ 2,000,000
  • s contains only lowercase English letters

Subtasks

Subtask #1 (20 points): 1 ≤ |s| ≤ 2000

Subtask #2 (80 points): original constraints

Sample Input 1 

5
ifchefisgoodforchess
fehcaskchefechohisvoice
hecfisaniceperson
letscallchefthefch
chooseaprogrammer

Sample Output 1 

lovely 2
lovely 3
lovely 1
lovely 3
normal

Explanation

Example case 1: We can select “fche” (the substring starting at index 2) or “chef” (index 3).

Example case 2: We can select “fehc” (index 1), “chef” (index 8) or “fech” (index 11).

Example case 3: We can only select “hecf” (index 1).

Example case 4: We can select “chef” (index 9), “hefc” (index 14), or “efch” (index 15).

Example case 5: No four contiguous characters can be reordered to form Chef’s name.

Chef And His Characters CodeChef Solution in JAVA

import java.util.*;
class Chef_Characters {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        while (t-- > 0) {
            String str = input.next();
            int count = 0;
            for (int i = 0; i < str.length() - 3; i++) {
                String temp = (str.substring(i, i + 4));
                if (condition(temp)) {
                    count++;
                }
            }
            if (count > 0) {
                System.out.println("lovely " + count);
            } else
                System.out.println("normal");
        }
        input.close();
    }
    private static boolean condition(String temp) {
        char arr1[] = temp.toCharArray();
        char arr2[] = { 'c', 'h', 'e', 'f' };
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }
}

Chef And His Characters CodeChef Solution in CPP

#include <bits/stdc++.h>
using namespace std;
int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string s,c="cefh";
	    cin>>s;
	    int i=0,ans=0;
	    if(s.length()<4)
        {
            cout<<"normal"<<endl;
            continue;
        }
	   for(i=0;i<s.length()-4+1;i++)
	   {
	       string temp=s.substr(i,4);
	       sort(temp.begin(),temp.end());
	       if(c==temp)
	       ans++;
	   }
	   if(ans)
	   cout<<"lovely"<<" "<<ans<<endl;
	   else
	   cout<<"normal"<<endl;
	}
	return 0;
}

Chef And His Characters CodeChef Solution in Python

for _ in range(int(input())):
    a=input()
    b=0
    for i in range(len(a)-3):
        c=a[i:i+4]
        if('c' in c and 'h' in c and 'e' in c and 'f' in c):
            b+=1
    if(b==0):
        print('normal')
    else:
        print('lovely',b)

Disclaimer: The above Problem (Chef And His Characters ) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring