#214

Shortest Palindrome

master · 1645 · lc hard +32 · verified · 42% accepted · 4,551 likes · top 23%

play →

Description

Given a string s, prepend the fewest characters needed to turn it into a palindrome and return the result.

Example 1:

Input: s = "aacecaaa"
Output: "aaacecaaa"

Example 2:

Input: s = "abcd"
Output: "dcbabcd"

Code

1
2
3