Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding 0647 Palindromic Substrings
  • Loading branch information
sailok committed Oct 10, 2020
commit 34514b98d408982b72d6f036c3564a70f205f31c
21 changes: 21 additions & 0 deletions LeetCode/0647_Palindromic_Substrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def countSubstrings(self, s: str) -> int:
N = len(s)
# declaring a DP matrix of size nxn
dp = [[0 for i in range(N)] for j in range(N)]
# looping through substring of every size and checking whether it is a valid substring
for l in range(N):
for i in range(N-l):
if l == 0:
dp[i][i] = 1
continue
if s[i] == s[i+l]:
if l == 1:
dp[i][i+l] = 1
elif dp[i+1][i+l-1] == 1:
dp[i][i+l] = 1
count = 0
for i in range(N):
for j in range(N):
count+=dp[i][j]
return count