You are given a string ‘str’, the task is to check the reverses of all possible substrings of ‘str’ are present in ‘str’ or not.
Examples:
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++
#include<bits/stdc++.h>
using namespace std;
bool isReversible(string str)
{
int i = 0, j = str.length()-1;
while (i < j)
{
if (str[i] != str[j])
return false ;
i++;
j--;
}
return true ;
}
int main()
{
string str= "aba" ;
if (isReversible(str))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isReversible(String str)
{
int i = 0 , j = str.length() - 1 ;
while (i < j)
{
if (str.charAt(i) != str.charAt(j))
return false ;
i++;
j--;
}
return true ;
}
public static void main (String[] args)
{
String str = "aba" ;
if (isReversible(str))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def isReversible( str ):
i = 0 ; j = len ( str ) - 1 ;
while (i < j):
if ( str [i] ! = str [j]):
return False ;
i + = 1 ;
j - = 1 ;
return True ;
str = "aba" ;
if (isReversible( str )):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
public static bool isReversible( string str)
{
int i = 0, j = str.Length - 1;
while (i < j)
{
if (str[i] != str[j])
{
return false ;
}
i++;
j--;
}
return true ;
}
public static void Main( string [] args)
{
string str = "aba" ;
if (isReversible(str))
{
Console.Write( "YES" );
}
else
{
Console.Write( "NO" );
}
}
}
|
Javascript
<script>
function isReversible(str)
{
var i = 0,
j = str.length - 1;
while (i < j)
{
if (str[i] != str[j])
return false ;
i++;
j--;
}
return true ;
}
var str = "aba" ;
if (isReversible(str))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time complexity: O(n) where n is length of the string.
Auxiliary space: O(1)
Another method to find a string is perfect reversible or Not:-
You can also check the string is perfect reversible or not by checking the string is palindromic or not. 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 the method to find a palindrome of a string:-
C++
#include<bits/stdc++.h>
using namespace std;
bool isReversible(string s)
{
string p = s;
reverse(p.begin(), p.end());
if (s == p) {
return true ;
}
return false ;
}
int main()
{
string str= "aba" ;
if (isReversible(str))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
C#
using System;
namespace ConsoleApp1 {
class GFG {
static void Main( string [] args)
{
string str = "aba" ;
if (IsReversible(str))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
static bool IsReversible( string s)
{
string p = s;
char [] arr = p.ToCharArray();
Array.Reverse(arr);
p = new string (arr);
return s == p;
}
}
}
|
Java
import java.util.*;
public class Main {
static boolean isReversible(String s)
{
String p
= new StringBuilder(s).reverse().toString();
if (s.equals(p)) {
return true ;
}
return false ;
}
public static void main(String[] args)
{
String str = "aba" ;
if (isReversible(str))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isReversible(s):
p = s[:: - 1 ]
if s = = p:
return True
return False
if __name__ = = "__main__" :
str = "aba"
if isReversible( str ):
print ( "YES" )
else :
print ( "NO" )
|
Javascript
function isReversible(s) {
let p = s.split( "" ).reverse().join( "" );
if (s == p) {
return true ;
}
return false ;
}
let str = "aba" ;
if (isReversible(str))
console.log( "YES" );
else
console.log( "NO" );
|
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Mar, 2023
Like Article
Save Article