Skip to content

Commit babc379

Browse files
authored
Merge pull request vJechsmayr#565 from sailok/feature/0647_Palindromic_Substrings
Adding 0647- Palindromic Substrings
2 parents 9b8dc7b + 34514b9 commit babc379

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
dp[i][i+l] = 1
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

Comments
 (0)