We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9d339f3 commit f0cf55fCopy full SHA for f0cf55f
palindromic-substrings/samthekorean.py
@@ -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