Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Perfect reversible string

  • Difficulty Level : Easy
  • Last Updated : 25 Mar, 2021

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

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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

Input : str = "aba"
Output: "YES"

Input : str = "abab"
Output: "NO"
// All substrings are "a", "b", "a", "b", "ab", 
// "ba", "ab", "aba", "bab", "abab" but reverse of
// "abab" is not present in str

A simple solution for this problem is to generate all possible substrings of ‘st’ and check if their reverse exist in the ‘str’ linearly.
An efficient solution for this problem is based on the fact that reverse of all substrings of ‘str’ will exist in ‘str’ if and only if the entire string ‘str’ is palindrome. We can justify this fact by considering the whole string, a reverse of it will exist only if it is palindrome. And if a string is palindrome, then all reverse of all substrings exist.



Below is implementation of above idea.  

C++




// C++ program to check if a string is perfect
// reversible or nor
#include<bits/stdc++.h>
using namespace std;
 
// This function basically checks if string is
// palindrome or not
bool isReversible(string str)
{
     int i = 0, j = str.length()-1;
 
     // iterate from left and right
     while (i < j)
     {
        if (str[i] != str[j])
            return false;
        i++;
        j--;
     }
     return true;
}
 
// Driver program to run the case
int main()
{
  string str="aba";
  if (isReversible(str))
      cout << "YES";
  else
      cout << "NO";
  return 0;
}

Java




// Java program to check
// if a string is perfect
// reversible or nor
import java.io.*;
 
class GFG
{
 
// This function basically
// checks if string is
// palindrome or not
static boolean isReversible(String str)
{
    int i = 0, j = str.length() - 1;
 
    // iterate from
    // left and right
    while (i < j)
    {
        if (str.charAt(i) != str.charAt(j))
            return false;
        i++;
        j--;
    }
    return true;
}
 
// Driver Code
public static void main (String[] args)
{
    String str = "aba";
    if (isReversible(str))
        System.out.print("YES");
    else
        System.out.print( "NO");
}
}
 
// This code is contributed
// by anuj_67.

Python3




# Python3 program to check if
# a string is perfect reversible or not
 
# This function basically checks
# if string is palindrome or not
def isReversible(str):
    i = 0; j = len(str) - 1;
 
    # iterate from left and right
    while (i < j):
        if (str[i] != str[j]):
            return False;
        i += 1;
        j -= 1;
    return True;
 
# Driver Code
str = "aba";
if (isReversible(str)):
    print("YES");
else:
    print("NO");
     
# This code is contributed by Princi Singh

C#




// C# program to check if a string
// is perfect reversible or nor
using System;
 
class GFG
{
 
// This function basically checks if
// string is palindrome or not
public static bool isReversible(string str)
{
    int i = 0, j = str.Length - 1;
 
    // iterate from left and right
    while (i < j)
    {
        if (str[i] != str[j])
        {
            return false;
        }
        i++;
        j--;
    }
    return true;
}
 
// Driver Code
public static void Main(string[] args)
{
    string str = "aba";
    if (isReversible(str))
    {
        Console.Write("YES");
    }
    else
    {
        Console.Write("NO");
    }
}
}
 
// This code is contributed
// by anuj_67

Javascript




<script>
 
// JavaScript program to check if a
// string is perfect reversible or not
 
// This function basically checks
// if string is palindrome or not
function isReversible(str)
{
    var i = 0,
    j = str.length - 1;
     
    // Iterate from left and right
    while (i < j)
    {
        if (str[i] != str[j])
            return false;
             
        i++;
        j--;
    }
    return true;
}
 
// Driver Code
var str = "aba";
 
if (isReversible(str))
    document.write("YES");
else
    document.write("NO");
     
// This code is contributed by rdtank   
     
</script>

Output: 

YES

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :