Home » Interview Questions » String Interview Questions » Perfect reversible string

Perfect reversible string


()

Check that if reverses of all possible substrings of input string are present in string or not. If present it’s a perfectly reversible string.

Example

a) Input string : abc
Output : NO
Here ab reverse ba is not present in abc.

b) Input string : aba
Output : Yes
Here a, b, ab, ba , aba present in string.

Time complexity : O(n)

Algorithm

Check if the string is palindrome or nott.

If string is palindrome it is perfectly reversible.

C++ Program

#include <bits/stdc++.h>

using namespace std;

//Check if palinddrome or not
//If palindrome reversible, else not 
bool isReversible(string str)
{
     int i = 0, j = str.length()-1;
     //Traverse from left and right
     while (i < j)
     {
        if (str[i] != str[j])
        {
            return false;
        }
        i++;
        j--;
     }
     return true;
}
 
//Main function
int main()
{
  string input_string="abab";
  if (isReversible(input_string))
  {
      cout<< "Perfectly reversible string";
  }
  else
  {  
      cout << "Not Perfectly reversible";
  }
  return 0;
}

Try It

 

 

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote Count

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

READ  Next Permutation
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions