Codechef : Problem name " Chef and Happy String " solution in C#

 ===================================Solution================================

using System;

public class Test

{

public static void Main()

{

    int t=Convert.ToInt32(Console.ReadLine());

for(int i=0;i<t;i++){

    string input=Console.ReadLine();

    bool flag=false;

    int count=0;

    for(int j=0;j<input.Length-3;j++)

    {

        string substr=input.Substring(j,3);

        foreach(var x in substr){

            if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')

            {                

                ++count;

                if(count==3){

                    flag=true;

                    break;

                }

            }

        }         

        count=0;            

    }

    

    if(flag)

    {

        Console.WriteLine("Happy");

    }

    else

        Console.WriteLine("Sad");

}

}

}

================================Explanation=================================
1.We have taken flag and count variables for internal calculations
2.First we need to run the loop till input_size-2 because when we create substring of size 3 it will cover till last character of input 
3.We will create substring by passing index and size of substring 
4.As we got the substring we will check for each of the 3 characters, if any one of them not vowel flag will be still false 
5.Flag will be only true when size of substring is 3 and for index 0 and 1 substring is vowel. We have kept one count variable to do the same. Once we are sure substring all chars are vowel and size of substring is 3 we will make flag true. 
6.Reset the count for next calculations of substring outside foreach loop where it ends.

Comments