Skip to content

Commit 3043f4e

Browse files
committed
- Palindromic Substrings DaleStudy#267
1 parent 34c4e88 commit 3043f4e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

palindromic-substrings/ayosecu.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
"""
3+
- Time Complexity: O(n^2), n = len(s)
4+
- Space Complexity: O(1)
5+
"""
6+
def countSubstrings(self, s: str) -> int:
7+
count = 0
8+
9+
def checkSide(l, r):
10+
cnt = 0
11+
while l >= 0 and r < len(s) and s[l] == s[r]:
12+
cnt += 1
13+
l -= 1
14+
r += 1
15+
return cnt
16+
17+
for i in range(len(s)):
18+
count += checkSide(i, i) # Odd case
19+
count += checkSide(i, i + 1) # Even case
20+
21+
return count
22+
23+
tc = [
24+
("abc", 3),
25+
("aaa", 6)
26+
]
27+
28+
sol = Solution()
29+
for i, (s, e) in enumerate(tc, 1):
30+
r = sol.countSubstrings(s)
31+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")

0 commit comments

Comments
 (0)