Perfect Reversible String

PROBLEM :

You are given a string ‘str’, the task is to check that reverses of all possible substrings of ‘str’ are present in ‘str’?

Input : str = "ab"
Output: 0
// all substrings are "a","b","ab" but reverse
// of "ab" is not present in str

Input : str = "aba"
Output: 1

Input:
The first line contains 'T' denoting the number of testcases. Then follows description of testcases.
Each case begins with a single integer N denoting the length of string. The next line contains the string s.


Output:
Print "1" if it is a palindrome else "1". (Without the double quotes)


Constraints:
1<=T<=30
1<=N<=100


Example:

Input:
2
3
aba
5
abcde

Output:
1
0

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

#include<iostream>
using namespace std;
int palindrom(char [],int ) ;
int main()
 {
int t,no,i ;
cin>>t ;
while(t--)
{
   cin>>no ;
 
   char str[no] ;
   for(i=0;i<no;i++)
       cin>>str[i] ;
     
   if(palindrom(str,no))
       cout<<1 ;
      else
       cout<<0 ;
   cout<<endl ;
}
return 0;
}

int palindrom(char str[],int no)
{
    int i,j ;
    i=0;
    j=no-1 ;
   
    while(i<j)
    {
        if(str[i]==str[j])
        {
            i++ ;
            j-- ;
        }
        else
            break ;
    }
   
    if(i<j)
        return 0 ;
    return 1 ;
}

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

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 )