647. Palindromic Substrings
string
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".解法思路
Last updated
string
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".Last updated
class Solution:
def countSubstrings(self, s: str) -> int:
res = 0
def expand_around_center(i,j):
nonlocal res
while i >=0 and j < len(s) and s[i] == s[j]:
i -= 1
j += 1
res += 1
for i in range(len(s)):
expand_around_center(i,i)
expand_around_center(i, i + 1)
return res