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 ''

  

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-11-27
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2021-06-17
  • 2021-08-06
  • 2021-10-28
  • 2021-10-11
  • 2021-05-18
  • 2021-10-12
相关资源
相似解决方案