Skip to content

Commit

Permalink
palindromic-substrings-added
Browse files Browse the repository at this point in the history
  • Loading branch information
NK-Works authored Nov 4, 2024
1 parent 5a87ba1 commit fadea9e
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def countPalindromicSubstrings(s):
n = len(s)
count = 0

def expandAroundCenter(left, right):
nonlocal count
while left >= 0 and right < n and s[left] == s[right]:
count += 1
left -= 1
right += 1

# Consider each character and each gap between characters as a center
for i in range(n):
expandAroundCenter(i, i) # Odd-length palindromes
expandAroundCenter(i, i + 1) # Even-length palindromes

return count

# Test the function
s = "abc"
print(f"Number of palindromic substrings in '{s}': {countPalindromicSubstrings(s)}")

s = "aaa"
print(f"Number of palindromic substrings in '{s}': {countPalindromicSubstrings(s)}")

0 comments on commit fadea9e

Please sign in to comment.