214 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".

Brute Force idea: check for matching substrings

Ex1:

abcd
dcba
abc
cba
ab
ba
a
a

Ex2:
adcbabcd
aacecaaa
aaacecaa
aacecaa
aacecaa

Last updated

Was this helpful?