Skip to content

Commit 2d8ab89

Browse files
committed
solve: palindromic substrings
1 parent 93b4acf commit 2d8ab89

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

palindromic-substrings/evan.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
def expand_around_center(left: int, right: int) -> int:
4+
count = 0
5+
6+
while left >= 0 and right < len(s) and s[left] == s[right]:
7+
count += 1
8+
left -= 1
9+
right += 1
10+
11+
return count
12+
13+
total_count = 0
14+
15+
for i in range(len(s)):
16+
# Odd-length palindromes (single character(s[i]) center)
17+
total_count += expand_around_center(i, i)
18+
# Even-length palindromes (two character(s[i], s[i+1])s center)
19+
total_count += expand_around_center(i, i + 1)
20+
21+
return total_count

0 commit comments

Comments
 (0)