Skip to content

Commit d96b82f

Browse files
committed
feat: add palindromic substrings solution
1 parent bae7af9 commit d96b82f

File tree

1 file changed

+23
-0
lines changed
  • palindromic-substrings

1 file changed

+23
-0
lines changed

palindromic-substrings/dm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def countPalindrome(self, s: str, left: int, right: int) -> int:
3+
result = 0
4+
5+
while left >= 0 and right < len(s) and s[left] == s[right]:
6+
result += 1
7+
left -= 1
8+
right += 1
9+
10+
return result
11+
12+
def countSubstrings(self, s: str) -> int:
13+
total_count = 0
14+
15+
for i in range(len(s)):
16+
left = right = i
17+
total_count += self.countPalindrome(s, left, right)
18+
19+
for i in range(len(s) - 1):
20+
left, right = i, i + 1
21+
total_count += self.countPalindrome(s, left, right)
22+
23+
return total_count

0 commit comments

Comments
 (0)