Given a string, check if it is a rotation of a palindrome. For example your function should return true for “aab” as it is a rotation of “aba”.
Examples:
Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any palindrome.
A Simple Solution is to take the input string, try every possible rotation of it and return true if a rotation is a palindrome. If no rotation is palindrome, then return false.
Following is C++ implementation of this approach.
Following is C++ implementation of this approach.
#include<iostream> #include<string> using namespace std; // A utility function to check if a string str is palindrome bool isPalindrome(string str) { // Start from leftmost and rightmost corners of str int l = 0; int h = str.length() - 1; // Keep comparing characters while they are same while (h > l) if (str[l++] != str[h--]) return false ; // If we reach here, then all characters were matching return true ; } // Function to chech if a given string is a rotation of a // palindrome. bool isRotationOfPalindrome(string str) { // If string iteself is palindrome if (isPalindrome(str)) return true ; // Now try all rotations one by one int n = str.length(); for ( int i = 0; i < n-1; i++) { string str1 = str.substr(i+1, n-i-1); string str2 = str.substr(0, i+1); // Check if this rotation is palindrome if (isPalindrome(str1.append(str2))) return true ; } return false ; } // Driver program to test above function int main() { cout << isRotationOfPalindrome( "aab" ) << endl; cout << isRotationOfPalindrome( "abcde" ) << endl; cout << isRotationOfPalindrome( "aaaad" ) << endl; return 0; } |
Output:
1 0 1
Time Complexity: O(n2)
Auxiliary Space: O(n) for storing rotations.
Note that the above algorithm can be optimized to work in O(1) extra space as we can rotate a string in O(n) time and O(1) extra space.
Auxiliary Space: O(n) for storing rotations.
Note that the above algorithm can be optimized to work in O(1) extra space as we can rotate a string in O(n) time and O(1) extra space.
An Optimized Solution can work in O(n) time. The idea is similar to this post. Following are steps.
1) Let the given string be ‘str’ and length of string be ‘n’. Create a temporary string ‘temp’ which is of size 2n and contains str followed by str again. For example, let str be “aab”, temp would be “aabaab”.
2) Now the problem reduces to find a palindromic substring of length n in str. If there is palindromic substring of length n, then return true, else return false.
We can find whether there is a palindromic substring of size n or not in O(n) time (See Longest palindromic substring)
1) Let the given string be ‘str’ and length of string be ‘n’. Create a temporary string ‘temp’ which is of size 2n and contains str followed by str again. For example, let str be “aab”, temp would be “aabaab”.
2) Now the problem reduces to find a palindromic substring of length n in str. If there is palindromic substring of length n, then return true, else return false.
We can find whether there is a palindromic substring of size n or not in O(n) time (See Longest palindromic substring)
This article is contributed Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
No comments:
Post a Comment