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 8b22e3f commit eda20caCopy full SHA for eda20ca
palindromic-substrings/sun912.py
@@ -0,0 +1,26 @@
1
+"""
2
+ TC: O(n^2)
3
+
4
+ TC in first try was O(n^3).
5
+ It is the improved codes from neetcodes
6
+ key point is that each character in loop is considered to middle one.
7
+ Ant then, check left end and right end.
8
9
10
+class Solution:
11
+ def countSubstrings(self, s: str) -> int:
12
+ result = 0
13
+ for i in range(len(s)):
14
+ result += self.countPalindrom(s, i, i)
15
+ result += self.countPalindrom(s, i, i+1)
16
+ return result
17
18
19
+ def countPalindrom(self, s, left_end, right_end):
20
21
+ while left_end >= 0 and right_end < len(s) and s[left_end] == s[right_end]:
22
+ result += 1
23
+ left_end -= 1
24
+ right_end += 1
25
26
0 commit comments