Skip to content

Commit 739893e

Browse files
committed
O(n^2) time and O(1) space using concept of expansion around the center.
1 parent 5d5504a commit 739893e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def dfs(self, left, right,s):
3+
final = 0
4+
while left >= 0 and right < len(s) and s[left] == s[right]:
5+
left -= 1
6+
right += 1
7+
final += 1
8+
return final
9+
10+
def countSubstrings(self, s: str) -> int:
11+
result = 0
12+
for i in range(len(s)):
13+
result += self.dfs(i,i,s)
14+
result += self.dfs(i,i+1,s)
15+
return result

0 commit comments

Comments
 (0)