-
Notifications
You must be signed in to change notification settings - Fork 0
/
0647__palindromic_substrings.py
127 lines (96 loc) · 3.39 KB
/
0647__palindromic_substrings.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
LeetCode: https://leetcode.com/problems/palindromic-substrings/
Given a string s, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
## Example 1
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
## Example 2
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
## Constraints
* 1 <= s.length <= 1000
* s consists of lowercase English letters.
"""
from functools import cache
from unittest import TestCase
class Solution(TestCase):
def test_example_1(self):
self.assertEqual(3, self.countSubstrings("abc"))
self.assertEqual(3, self.countSubstringsDP("abc"))
self.assertEqual(3, self.countSubstringsFirstApproach("abc"))
def test_example_2(self):
self.assertEqual(6, self.countSubstrings("aaa"))
self.assertEqual(6, self.countSubstringsDP("aaa"))
self.assertEqual(6, self.countSubstringsFirstApproach("aaa"))
def countSubstringsDP(self, s: str) -> int:
count = 0
# dp[i, j] = is_palindrome(s[i:j])
dp = [[False] * len(s) for _ in range(len(s))]
# base case: 1 letter
for i in range(len(s)):
count += 1
dp[i][i] = True
# base case: 2 letters
for i in range(len(s)):
left, right = i, i + 1
if right < len(s) and s[left] == s[right]:
count += 1
dp[left][right] = True
# everything else
for length in range(3, len(s) + 1):
i = 0
j = length - 1
while j < len(s):
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
if dp[i][j]:
count += 1
i += 1
j += 1
return count
def countSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)):
# 1 character
count += 1
# odd length
left, right = i - 1, i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
# even length
left, right = i, i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count
def countSubstringsFirstApproach(self, s: str) -> int:
def is_palindrome(string: str) -> bool:
left, right = 0, len(string) - 1
while left < right:
if string[left] != string[right]:
return False
left += 1
right -= 1
return True
@cache
def is_palindrome_cached(string: str) -> bool:
if len(string) <= 1:
return True
return string[0] == string[-1] and is_palindrome_cached(string[1:-1])
count = 0
options = []
for i in range(len(s) - 1, -1, -1):
for idx, sub in enumerate(options):
candidate = s[i] + sub
if is_palindrome_cached(candidate):
count += 1
options[idx] = candidate
options.append(s[i])
count += 1
return count