[leetcode]Shortest Palindrome

O(n^2)的方法,最后一个case超时。需要用kmp方法或者manacher方法才能O(n),先忽略了。

class Solution:
    def isPalindrome(self, sub: str) -> bool:
        for i in range(len(sub) // 2):
            if sub[i] != sub[len(sub) - i - 1]:
                return False
        return True
        
    def shortestPalindrome(self, s: str) -> str:
        for i in range(len(s)-1,-1,-1):
            if self.isPalindrome(s[:i+1]):
                palindrome = s[i+1:][::-1] + s
                return palindrome
                    
        return ''

  

原文地址:https://www.cnblogs.com/lautsie/p/12273851.html