Chef and Happy String CodeChef Solution

Problem – Chef and Happy String CodeChef Solution

Chef has a string S with him. Chef is happy if the string contains a contiguous substring of length strictly greater than 2 in which all its characters are vowels.

Determine whether Chef is happy or not.

Note that, in english alphabet, vowels are aeio, and u.

Input Format

  • First line will contain T, number of test cases. Then the test cases follow.
  • Each test case contains of a single line of input, a string S.

Output Format

For each test case, if Chef is happy, print HAPPY else print SAD.

You may print each character of the string in uppercase or lowercase (for example, the strings hAppYHappyhaPpY, and HAPPY will all be treated as identical).

Constraints

  • 1≤T≤1000
  • 3≤∣S∣≤1000, where ∣S∣ is the length of S.
  • S will only contain lowercase English letters.

Sample 1:

Input:
4
aeiou
abxy
aebcdefghij
abcdeeafg
Output:
Happy
Sad
Sad
Happy

Explanation:

Test case 1: Since the string aeiou is a contiguous substring and consists of all vowels and has a length >2, Chef is happy.

Test case 2: Since none of the contiguous substrings of the string consist of all vowels and have a length >2, Chef is sad.

Test case 3: Since none of the contiguous substrings of the string consist of all vowels and have a length >2, Chef is sad.

Test case 4: Since the string eea is a contiguous substring and consists of all vowels and has a length >2, Chef is happy.

Chef and Happy String CodeChef Solution in 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
		int t;
		Scanner scan=new Scanner(System.in);
		t=scan.nextInt();
		while(t-->0){
		    String str;
		    int count=0;
		    str=scan.next();
		    int j=str.length();
		    for(int i=0;i<j;i++){
		        if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'){
		            count++;
		        }
		        else{
		            count=0;
		        }
		        
		        if(count==3){
		            System.out.println("Happy");
		            break;
		        }
		    }
		    if(count!=3){
		        System.out.println("Sad");
		    }
		}
	}
}

Chef and Happy String CodeChef Solution in C++17

#include <iostream>
#include<string>
using namespace std;

int isVowel(char ch){
    return ch=='a'|| ch=='e' || ch=='i' || ch=='o' || ch=='u';
}

int main() {
	int t;
	cin>>t;
	while(t--)
	{
        string s;
        int flag=0;
        cin>>s;
        for(int i=0;i<s.length()-2;i++){
            if(isVowel(s[i])&&isVowel(s[i+1])&&isVowel(s[i+2])){
            flag=1;
            break;}
        }
        if(flag){
            cout<<"Happy"<<endl;
        }
        else{
            cout<<"Sad"<<endl;
        }
        
	}
	return 0;
}

Chef and Happy String CodeChef Solution in Pyth 3

n = int(input())
while(n):
    list1 = ['a', 'e', 'i', 'o', 'u']
    n = n-1
    count = 0
    str1 = input()
    for i in str1:
        if i in list1:
            count = count+1
            if count > 2:
                break
        else:
            count = 0
    if count > 2:
        print("Happy")
    else:
        print('Sad')
Chef and Happy String CodeChef Solution Review:

In our experience, we suggest you solve this Chef and Happy String CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Chef and Happy String CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Chef and Happy String CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *