Check if given string is a rotated palindrome or not

Given a string, check if it is a rotated palindrome or not.

 

For example,
CBAABCD is a rotated palindrome as it is rotation of palindrome ABCDCBA
BAABCC is a rotated palindrome as it is rotation of palindrome ABCCBA

 


 

Naive solution would be to consider all rotations of the given string and check if any rotation is a palindrome or not. If we have found a rotation that is a palindrome, we return true else we return false.

 
C++ implementation –
 

Download   Run Code

Output:

Yes

 

Approach 2:

The problem is similar to finding Longest Palindromic Substring problem. Let the given string be S of length m. The idea is to concatenate the given string with itself i.e (S = S + S) and find palindromic substring of length m in the modified string (S + S). If palindromic substring of length m exists in the modified string, we return true else we return false.

 
C++ implementation –
 

Download   Run Code

Output:

Yes

 

Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂