Perfect reversible string - GeeksforGeeks
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.
Read full article from Perfect reversible string - GeeksforGeeks
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.
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.
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
;
}