Skip to content

Commit f0cf55f

Browse files
committed
solve : palindromic substrings
1 parent 9d339f3 commit f0cf55f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# O(n^2)
2+
# O(1)
3+
class Solution:
4+
def countSubstrings(self, s: str) -> int:
5+
length, total_palindromes = len(s), 0
6+
7+
def countPalindromes(left: int, right: int) -> int:
8+
count = 0
9+
while left >= 0 and right < length and s[left] == s[right]:
10+
left -= 1
11+
right += 1
12+
count += 1
13+
return count
14+
15+
for i in range(length):
16+
total_palindromes += countPalindromes(i, i + 1) # even length palindromes
17+
total_palindromes += countPalindromes(i, i) # odd length palindromes
18+
19+
return total_palindromes

0 commit comments

Comments
 (0)