-
Notifications
You must be signed in to change notification settings - Fork 340
/
palindromeRecursion
53 lines (44 loc) · 1.3 KB
/
palindromeRecursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// Recursive function to check if `str[low…high]` is a palindrome or not
bool isPalindrome(string str, int low, int high)
{
return (low >= high) || (str[low] == str[high] &&
isPalindrome(str, low + 1, high - 1));
}
// Function to check if a given string is a rotated palindrome or not
bool isRotatedPalindrome(string str)
{
// length of the given string
int n = str.length();
// check for all rotations of the given string if it
// is palindrome or not
for (int i = 0; i < n; i++)
{
// in-place rotate the string by 1 unit
rotate(str.begin(), str.begin() + 1, str.end());
// rotate(str.rbegin(), str.rbegin() + 1, str.rend());
// return true if the rotation is a palindrome
if (isPalindrome(str, 0, n - 1)) {
return true;
}
}
// return false if no rotation is a palindrome
return false;
}
int main()
{
// palindromic string
string str = "ABCDCBA";
// rotate it by 2 units
rotate(str.begin(), str.begin() + 2, str.end());
if (isRotatedPalindrome(str)) {
cout << "The string is a rotated palindrome";
}
else {
cout << "The string is not a rotated palindrome";
}
return 0;
}