214.Shortest-Palindrome

214. Shortest Palindrome

题目地址

https://leetcode.com/problems/shortest-palindrome/arrow-up-right

题目描述

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"

代码

Approach # Brute Force

Time complexity: O(n^2)

Approach #1 Two pointers and recursion

Approach #3 KMP

https://leetcode.com/problems/shortest-palindrome/discuss/60113/Clean-KMP-solution-with-super-detailed-explanationarrow-up-right

Last updated

Was this helpful?