LC647 - Palindromic Substrings
Problem
Example
Solution
Expand Palindrome from Centre
def expand(self, s: str, i, j):
lbound = i
rbound = j
subcount = 0
# expand if bounds don't reach end
while lbound >= 0 and rbound < len(s) and s[lbound] == s[rbound]:
lbound -= 1
rbound += 1
subcount += 1
# return number of palindromes found
return subcount
def countSubstrings(self, s: str) -> int:
odd = 0
even = 0
for i in range(len(s)):
# expand for odd length palindromes
odd += self.expand(s, i, i)
# expand for even length palindromes
even += self.expand(s, i, i + 1)
return odd + evenLast updated