题解 | #回文子串的数量#

回文子串的数量

https://www.nowcoder.com/practice/3e8b48c812864b0eabba0b8b25867738

来自chatgpt的答案,手动狗头。。。

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return int整型
#
class Solution:
    def Substrings(self , str: str) -> int:
        # write code here
        n = len(str)
        ans = 0
        for i in range(2 * n - 1):
            left = i // 2
            right = left + i % 2
            while left >= 0 and right < n and str[left] == str[right]:
                ans += 1
                left -= 1
                right += 1
        return ans

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务