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: string shortestPalindrome(string s) { string t = s; reverse(t.begin(), t.end()); int n = s.size(), i = 0; for (i = n; i >= 0; --i) { if (s.substr(0, i) == t.substr(n - i,i)) { break; } } return t.substr(0, n - i) + s; } };