Skip to content

Commit

Permalink
Merge pull request #1060 from NK-Works/palindromic-substrings
Browse files Browse the repository at this point in the history
Palindromic Substrings Added
  • Loading branch information
UTSAVS26 authored Nov 4, 2024
2 parents e129476 + 89a758a commit eb5fcb3
Show file tree
Hide file tree
Showing 2 changed files with 25 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)}")
1 change: 1 addition & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* Hard-Dp-Problems
* [Cherry-Pick-Algo](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/cherry-pick-algo.py)
* [Levenshtein-Distance](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/levenshtein-distance.py)
* [Palindromic-Substrings](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/palindromic-substrings.py)
* [Regular-Expression-Matching](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py)
* Medium-Dp-Problems
* [Coin-Change](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/coin-change.py)
Expand Down

0 comments on commit eb5fcb3

Please sign in to comment.