VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-given-string-rotation-palindrome/

⇱ Check if a given string is a rotation of a palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a given string is a rotation of a palindrome

Last Updated : 14 Jun, 2026

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.

[Brute-Force Approach] Try All Possible Rotations - O(n^2) Time and O(n) Space

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.


Output
1

[Expected Approach] Using Manacher's Algorithm - O(n) Time and O(n) Space

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:

  • Concatenate the original string with itself to simulate all possible rotations.
  • Preprocess the string by inserting special characters like '#' to handle even-length palindromes.
  • Initialize a length array P to store the radius of the palindrome centered at each index.
  • Use Manacher’s algorithm to expand around each center and fill the P array efficiently.
  • For every center, check if P[i] is at least equal to the original string's length.
  • If such a center exists, ensure the corresponding palindrome lies within the first 2n characters.
  • Return 1 if found, otherwise after the loop ends, return 0 as no valid rotation exists.

Output
1


Comment
Article Tags: