-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path647.py
68 lines (54 loc) · 1.88 KB
/
647.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import unittest
# Name: Palindromic Substrings
# Link: https://leetcode.com/problems/palindromic-substrings/
# Method: Expand around centre (dp alternative included)
# Time: O(n^2)
# Space: O(1)
# Difficulty: Medium
class Solution:
def countSubstrings_dp(self, s: str) -> int:
n = len(s)
dp = [[0 for _ in range(n)] for _ in range(n)]
for i in range(len(s)):
dp[i][i] = 1
palindromes = 0
for i in range(n - 1, -1, -1):
for j in range(i, n):
dp[i][j] = (s[i] == s[j]) and (i + 2 > j or dp[i + 1][j - 1])
palindromes += dp[i][j]
return palindromes
def countSubstrings(self, s: str) -> int:
return sum(
self.palindrome_count_around(s, i, i)
+ self.palindrome_count_around(s, i, i + 1)
for i in range(len(s))
)
@staticmethod
def palindrome_count_around(s: str, low: int, high: int):
ans = 0
while 0 <= low and high < len(s):
if s[low] != s[high]:
break
ans += 1
low -= 1
high += 1
return ans
class TestPalindromeCount(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_one_letter(self):
s = "a"
expected_ans = 1
self.assertEqual(self.sol.countSubstrings(s), expected_ans)
self.assertEqual(self.sol.countSubstrings_dp(s), expected_ans)
def test_lc_example(self):
s = "aaa"
expected_ans = 6
self.assertEqual(self.sol.countSubstrings(s), expected_ans)
self.assertEqual(self.sol.countSubstrings_dp(s), expected_ans)
def test_longer(self):
s = "afcfafcfadamedane"
expected_ans = 26
self.assertEqual(self.sol.countSubstrings(s), expected_ans)
self.assertEqual(self.sol.countSubstrings_dp(s), expected_ans)
unittest.main()