Skip to content

Commit

Permalink
O(n^2) time and O(1) space using concept of expansion around the center.
Browse files Browse the repository at this point in the history
  • Loading branch information
149ps committed Feb 10, 2024
1 parent 5d5504a commit 739893e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 647. Palindromic Substrings/647. Palindromic Substrings_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def dfs(self, left, right,s):
final = 0
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
final += 1
return final

def countSubstrings(self, s: str) -> int:
result = 0
for i in range(len(s)):
result += self.dfs(i,i,s)
result += self.dfs(i,i+1,s)
return result

0 comments on commit 739893e

Please sign in to comment.