Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
Example 1:
Input:"aacecaaa"
Output:"aaacecaaa"
Example 2:
Input: "abcd"
Output:
"dcbabcd"
class Solution
{public:
bool getResult(string s,int left,int right)
{
if(left==right) return true;
while(left<right){
if(s[left]!=s[right])
return false;
else
{
left++;
right--;
}
}
return true;
}
string shortestPalindrome(string s)
{
string result="";
int left=0;
int right=s.size()-1;
while(left<=right)
{
if(s[left]==s[right]&&getResult(s,left,right))
break;
elseright--;
}
for(int i=s.size()-1;i>right;i--) result+=s[i];
result+=s;
return result;
}
};