题目:
Shortest Palindrome
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.
For example:
Given "aacecaaa"
, return "aaacecaaa"
.
Given "abcd"
, return "dcbabcd"
.
分析:
利用进行求解。时间复杂度O(n)。空间复杂度O(n).
class Solution {public: string shortestPalindrome(string s) { if(s.size()<=1) return s; string str(s.size()*2+1,'#'); for(int i=0,j=1;i