There was an error while loading. Please reload this page.
2 parents 9b8dc7b + 34514b9 commit babc379Copy full SHA for babc379
LeetCode/0647_Palindromic_Substrings.py
@@ -0,0 +1,21 @@
1
+class Solution:
2
+ def countSubstrings(self, s: str) -> int:
3
+ N = len(s)
4
+ # declaring a DP matrix of size nxn
5
+ dp = [[0 for i in range(N)] for j in range(N)]
6
+ # looping through substring of every size and checking whether it is a valid substring
7
+ for l in range(N):
8
+ for i in range(N-l):
9
+ if l == 0:
10
+ dp[i][i] = 1
11
+ continue
12
+ if s[i] == s[i+l]:
13
+ if l == 1:
14
+ dp[i][i+l] = 1
15
+ elif dp[i+1][i+l-1] == 1:
16
17
+ count = 0
18
+ for i in range(N):
19
+ for j in range(N):
20
+ count+=dp[i][j]
21
+ return count
0 commit comments