1class Solution:2def shortestPalindrome(self, s: str) -> str:34end = 056# if the string itself is a palindrome return it7if s == s[::-1]:8return s910# Otherwise find the end index of the longest palindrome that starts11# from the first character of the string1213for i in range(len(s) + 1):14if s[:i] == s[:i][::-1]:15end = i - 11617# return the string with the remaining characters other than18# the palindrome reversed and added at the beginning1920return (s[end + 1 :][::-1]) + s