serega1112

647

Feb 28th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. class Solution:
  2.     def countSubstrings(self, s: str) -> int:
  3.         n = len(s)
  4.         dp = [[False] * n for _ in range(n)]
  5.         res = 0
  6.         for i in range(n):
  7.             dp[i][i] = True
  8.             res += 1
  9.             if i + 1 < n and s[i] == s[i+1]:
  10.                 dp[i][i+1] = True
  11.                 res += 1
  12.                
  13.         for i in range(n-2, -1, -1):
  14.             for j in range(i+1, n):
  15.                 if s[i] == s[j] and dp[i+1][j-1]:
  16.                     dp[i][j] = True
  17.                     res += 1
  18.                    
  19.         return res
Advertisement
Add Comment
Please, Sign In to add comment