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