![]() |
VOOZH | about |
Given a string s, the task is is to check whether s can be obtained by rotating a palindrome.
Examples:
Input: s = "aaaab"
Output: 1
Explanation: The rotation "aabaa" is a palindrome and can be rotated to form "aaaab", so output is 1.Input: s = "abcd"
Output: 0
Explanation: No rotation of any palindrome can form "abcde", so output is 0.Input: s = "aab"
Output: 1
Explanation: The palindrome aba can be rotated to form "aab", so the given string is a rotated palindrome.
Table of Content
The idea is to check if a rotation of the string can form a palindrome. The key thought is that any rotation of a string is always a substring of the original string concatenated with itself. So, we generate all possible rotated versions by sliding a window of size n over the concatenated string and check if any of them is a palindrome using two-pointer technique. If any such substring is found, we return 1, else 0.
1
The idea is to detect any rotation of a palindrome by first doubling the string to simulate all rotations. The thought process is based on Manacher's Algorithm, which efficiently finds longest palindromic substrings in linear time. By checking if any palindromic substring of length >= original string exists in the doubled string, we can confirm a rotated palindrome. This avoids checking every rotation explicitly, leveraging palindrome expansion symmetry.
Steps to implement the above idea:
1