From 34514b98d408982b72d6f036c3564a70f205f31c Mon Sep 17 00:00:00 2001 From: sailok Date: Sat, 10 Oct 2020 12:50:36 +0530 Subject: [PATCH] Adding 0647 Palindromic Substrings --- LeetCode/0647_Palindromic_Substrings.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LeetCode/0647_Palindromic_Substrings.py diff --git a/LeetCode/0647_Palindromic_Substrings.py b/LeetCode/0647_Palindromic_Substrings.py new file mode 100644 index 0000000..d1f8b9e --- /dev/null +++ b/LeetCode/0647_Palindromic_Substrings.py @@ -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 \ No newline at end of file