Palindromic Substrings

题目

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

总结

这个问题和求最大回文串是类似的,因为在求最大回文串的过程中我们就能得到答案。
解法一:
字符串的每个位置都可能是回文的中心,对每个位置求出其最长的回文即可,在求最长回文的过程中,记录下回文的个数。
代码:

class Solution:
    def countSubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
        count = len(s)
        for i in range(len(s)):
            start = i - 1
            end = i + 1
            while start >= 0 and end < len(s) and s[start] == s[end]:
                count += 1
                start -= 1
                end += 1
                
            start = i
            end = i + 1
            while start >= 0 and end < len(s) and s[start] == s[end]:
                count += 1
                start -= 1
                end += 1
        return count
            

时间复杂度为O(n^2),空间复杂度为O(1)

解法二:
思考一个问题,在求一个位置最长回文的时候,能不能利用到前面的最长回文信息?
答案是可以,假设之前有一个点的位置为center,他的最长回文半径长度为d
在求新的点的最长回文时,可以大致分为两种情况:

  1. 由于在center的d范围内两边是完全对称的,如果有i<d满足center-i处的最长回文长度仍然小于(center-d,center+d)的范围(不包含边界)。由对称性我们可以确定center+i处的最长回文长度与center-i处相等
  2. 如果有i<d满足center-i处的最长回文长度超过了(center-d,center+d)的范围(包含边界),在求这个点的回文长度时,无须从0开始,可直接从center+d-i开始(因为又对称性我们可以确定i的这个范围内是一个回文,只需扩大判断周围即可)
    这样我们就能减少时间复杂度到O(N)
    代码:
class Solution:
    def countSubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
    
        center = 0
        newS = ['|']
        for c in s:
            newS += [c, '|']
            
        lps = [0] * len(newS)
        numOfPad = 0
        
        def getLps4Center(center, init):
            count = init
            left = center - init - 1
            right = center + init + 1
            while right < len(newS) and newS[left] == newS[right]:
                count += 1
                left -= 1
                right += 1
            return count
        
        for i in range(1, len(lps)):
            if i <= center + lps[center]: 
                centerRight = center + lps[center]
                currentLeft = 2 * center - i
                if lps[currentLeft] < centerRight - i or (lps[currentLeft] == centerRight - i and centerRight == len(newS) - 1):
                    lps[i] = lps[currentLeft]
                else:
                    lps[i] = getLps4Center(i, centerRight - i)
                    center = i
                
            else:
                lps[i] = getLps4Center(i, 0)
                center = i
            numOfPad += (lps[i] +  i % 2) // 2
        
        return numOfPad
                

参考博文:
manachers-algorithm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值