- 23.5%
https://leetcode.com/problems/shortest-palindrome/#/description
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.
1 | For example: |
此处需要思考
https://discuss.leetcode.com/topic/14526/c-8-ms-kmp-based-o-n-time-o-n-memory-solution
C++ 8 ms KMP-based O(n) time & O(n) memory solution
We can construct the following string and run KMP algorithm on it:
(s) + (some symbol not present in s) + (reversed string)
After running KMP on that string as result we get a vector p with values of a prefix function for each character (for definition of a prefix function see KMP algorithm description). We are only interested in the last value because it shows us the largest suffix of the reversed string that matches the prefix of the original string. So basically all we left to do is to add the first k characters of the reversed string to the original string, where k is a difference between original string size and the prefix function for the last character of a constructed string.
1 | class Solution { |
https://discuss.leetcode.com/topic/14770/my-easily-understandable-but-time-consuming-c-solution
My easily understandable but time consuming C++ solution
The key idea is to first reverse the string, then check the max length from n to 0
1 | class Solution { |
Accepted 4ms c++ solution, different with KMP-based solution and easy understand.
For this problem, KMP-based solution is a very typical and classic O(n) solution. Here is a different solution, it’s also O(n), and I think it is more easy to understand.
In order to slove this problem, the key is to get the length of the longest palindromic prefix substring. if the length of s is len, and the length of the longest palindromic prefix substring is longest, the remaining substring will be s.substr(longest, len - longest), than we should reverse the remaining substring and adding it in front of s.
For example, if s is “abacbbcda”, so the longest palindromic prefix substring is “aba”(not “cbbc” because it’s not prefix string), and the remaining substring is “cbbcda”, we reverse the remaining substring and get “adcbbc”, so the result is “adcbbc” + “abacbbcda”.
The follow is my c++ solution, only 4ms. Please note that the condition in for loop is begin <= len / 2 instead of begin < len, because if begin > len / 2, the substring can not be prefix string, so there is no need to continue.
Update: I made wrong analysis, the complexity is O(N^2) but not O(N). Thanks very much for Sammax’s reminder.
1 | class Solution { |
262ms, 29.90%, October 14, 2016
https://discuss.leetcode.com/topic/14542/ac-in-288-ms-simple-brute-force
AC in 288 ms, simple brute force
1 | def shortestPalindrome(self, s): |
Example: s = dedcba. Then r = abcded and I try these overlays (the part in (…) is the prefix I cut off, I just include it in the display for better understanding):
1 | s dedcba |
7ms, 69.02%, October 14, 2016
https://discuss.leetcode.com/topic/21068/my-7-lines-recursive-java-solution
1 | public class Solution { |