Saturday 16 June 2018

Monk Teaches Palindrome

Monk introduces the concept of palindrome saying,"A palindrome is a sequence of characters which reads the same backward or forward."
Now, since he loves things to be binary, he asks you to find whether the given string is palindrome or not. If a given string is palindrome, you need to state that it is even palindrome (palindrome with even length) or odd palindrome (palindrome with odd length).
Input:
The first line consists of T, denoting the number of test cases.
Next follow T lines, each line consisting of a string of lowercase English alphabets.
Output:
For each string , you need to find whether it is palindrome or not.
If it is not a palindrome, print NO.
If it is a palindrome, print YES followed by a space; then print EVEN it is an even palindrome else print ODD.
Output for each string should be in a separate line.
See the sample output for clarification.
Constraints:
1T50
1length of string105
SAMPLE INPUT
 
3
abc
abba
aba
SAMPLE OUTPUT
 
NO
YES EVEN
YES ODD
Explanation
The first string is not a palindrome.
The second and third strings are palindromes of even and odd lengths respectively.






using System;

public class Program
{
    public static void Main(string[] args)
    {
    int tc= int.Parse(Console.ReadLine());
    
    while(tc>0)
    {
        string str= Console.ReadLine();
        int l= str.Length;
        int lp = l/2-1;
        bool isP=true;
        string strR="NO";
        for(int i=0;i<=lp;i++)
        {
            if(!(str.Substring(i,1)==str.Substring(l-1-i,1)))
            {
                isP=false;
                break;
            }
            
        }
        if(isP)
        {
          if(l%2==0)
          {
              strR="YES EVEN";
          }
          else
          {
              strR="YES ODD";
          }
        }
        
        Console.WriteLine(strR);
        tc--;
    }
}
}

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0