Task
Given a string, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.
Example
For st = "abcdc"
, the output should be
buildPalindrome(st) = "abcdcba"
.
Input/Output
-
[execution time limit]
4 seconds (py3) -
[input] string st
A string consisting of lowercase English letters.
Guaranteed constraints:3 ≤ st.length ≤ 10
. -
[output] string
My Solution
def buildPalindrome(st):
maxIndex = -1
for i in range(2, len(st) + 1):
if st[-i:] == st[:-i - 1:-1]:
maxIndex = -i
return st + st[:maxIndex][::-1]