Advertisement
bobbye

Palindromic Substrings

Apr 16th, 2024 (edited)
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. class Solution:
  2.     def countSubstrings(self, s: str) -> int:
  3.         res = 0
  4.  
  5.         def isPalindrome(l,r):
  6.             count = 0
  7.             while l >= 0 and r <len(s) and s[l] == s[r]:
  8.                 count += 1
  9.                 l -= 1
  10.                 r += 1
  11.             return count
  12.  
  13.         for i in range(len(s)):
  14.             res += isPalindrome(i, i) + isPalindrome(i, i+1)
  15.  
  16.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement