Leetcode 647 Solution

This article provides solution to leetcode question 647 (palindromic-substrings)

https://leetcode.com/problems/palindromic-substrings

Solution

class Solution:
    def countSubstrings(self, s: str) -> int:
        n = len(s)

        dp = [[0 for _ in range(n)] for _ in range(n)]
        for i in range(n):
            dp[i][i] = 1

        for k in range(1, n):
            for i in range(0, n - k):
                j = i + k
                if s[i] == s[j]:
                    if i + 1 <= j - 1:
                        dp[i][j] = dp[i + 1][j - 1]
                    else:
                        dp[i][j] = 1

        return sum([sum(row) for row in dp])