Related LeetCode - Longest Palindromic Substring
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”.
http://massivealgorithms.blogspot.com/2014/06/leetcode-longest-palindromic-substring.html
http://www.techiedelight.com/check-given-string-rotated-palindrome-not/
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.
http://www.geeksforgeeks.org/recursive-function-check-string-palindrome/
Read full article from Check if a given string is a rotation of a palindrome | GeeksforGeeks
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”.
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)
Manacher’s Algorithm: It takes advantages of palindrome's symmetric property and avoids some unnecessary computations.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)
http://massivealgorithms.blogspot.com/2014/06/leetcode-longest-palindromic-substring.html
http://www.techiedelight.com/check-given-string-rotated-palindrome-not/
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.
// expand in both directions of low and high to find
// palindrome of length k
bool expand(string str, int low, int high, int k)
{
// run till str[low.high] is a palindrome
while (low >= 0 && high < str.length() && (str[low] == str[high]))
{
// return true palindrome of length k is found
if (high - low + 1 == k)
return true;
// expand in both directions
low--, high++;
}
// return false if palindrome of length k is not found
return false;
}
// Function to check if palindromic substring of length k exists or not
bool LongestPalindromicSubstring(string str, int k)
{
for (int i = 0; i < str.length() - 1; i++)
// check if odd length or even length palindrome of length k
// exists which have str[i] as its mid point
if (expand(str, i, i, k) || expand(str, i, i + 1, k))
return true;
return false;
}
// Function to check if given string is a rotated palindrome or not
bool isRotatedPalindrome(string str)
{
// length of given string
int m = str.length();
// return true if longest palindromic substring of length m
// exists in the string (str + str)
return LongestPalindromicSubstring(str + str, m);
}
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.
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
;
}
http://www.geeksforgeeks.org/recursive-function-check-string-palindrome/
bool
isPalRec(
char
str[],
int
s,
int
e)
{
// If there is only one character
if
(s == e)
return
true
;
// If first and last characters do not match
if
(str[s] != str[e])
return
false
;
// If there are more than two characters,
// check if middle substring is also
// palindrome or not.
if
(s < e+1)
return
isPalRec(str, s+1, e-1);
return
true
;
}
void
isPalindrome(
char
str[])
{
// Start from leftmost and rightmost corners of str
int
l = 0;
int
h =
strlen
(str) - 1;
// Keep comparing characters while they are same
while
(h > l)
{
if
(str[l++] != str[h--])
{
printf
(
"%s is Not Palindrome\n"
, str);
return
;
}
}
printf
(
"%s is palindrome\n"
, str);
}