Good String

PROBLEM :

Given a string S of length N, you have to tell whether it is good or not. A good string is one where the distance between any two adjacent character is exactly 1. Consider that the alphabets are arranged in cyclic manner from 'a' to 'z'. Hence, distance between any character 'i' and 'j' will be defined as minimum number of steps it takes 'i' to reach 'j'. Here, character 'i' can start moving clockwise or anti-clockwise in order to reach at position where character 'j' is placed. Your task is simple, where you just need to print "YES" or "NO" (without quotes) depending on whether the given string is Good or not


Input:
First line of the input contains T denoting the number of test cases.Then T lines follow. Each line contains a string S.


Output:
Print  the answer for each testcase in a separate line.


Constraints:

1=T=50
1=|S|=50

Note: S contains only lowercase alphabetic characters


Input:
3
aaa
cbc
bcd

Output:
NO
YES
YES

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
string Good_String(string ) ;
int main()
 {
int t ;
string str ;
cin>>t ;
while(t--)
{
   cin>>str ;
 
   str=Good_String(str) ;
   cout<<str<<endl ;
}
return 0;
}

string Good_String(string str)
{
    int l ;
    l=str.length() ;
   
    int i ;
    for(i=0;i<l-1;i++)
    {
        if((abs(str[i]-str[i+1])!=25)&&(abs(str[i]-str[i+1])!=1))
            return "NO" ;
    }
    return "YES" ;
}                                                          

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )