其他分享
首页 > 其他分享> > 找到 字符串中最长的回文子串

找到 字符串中最长的回文子串

作者:互联网

如题:

class Solution:
    def expandAroundCenter(self, s, left, right):
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return left + 1, right - 1

    def longestPalindrome(self, s):
        start, end = 0, 0
        for i in range(len(s)):
            left1, right1 = self.expandAroundCenter(s, i, i)
            left2, right2 = self.expandAroundCenter(s, i, i + 1)
            if right1 - left1 > end - start:
                start, end = left1, right1
            if right2 - left2 > end - start:
                start, end = left2, right2
        return s[start: end + 1]

str="asdasadasius"
a=Solution()
b=a.longestPalindrome(str)
print(b,len(b))

可以返回最长回文串和其长度在这里插入图片描述

标签:子串,right,end,left1,self,start,字符串,left,回文
来源: https://blog.csdn.net/kaede_xiao/article/details/122050186