-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpalindrome-partitioning-ii.py
238 lines (171 loc) · 7.47 KB
/
palindrome-partitioning-ii.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from typing import List
from collections import defaultdict
from functools import lru_cache
class Solution:
@staticmethod
def max_palindromes(string: str) -> List[List[int]]:
def palindrome_here_odd(pos: int, offset: int) -> int:
left, right = pos - offset, pos + offset
while left >= 0 and right < len(string) and string[left] == string[right]:
left -= 1
right += 1
return right - pos
def palindrome_here_even(pos: int, offset: int) -> int:
left, right = pos - 1 - offset, pos + offset
while left >= 0 and right < len(string) and string[left] == string[right]:
left -= 1
right += 1
return right - pos
odd_palindromes: List[int] = []
rightmost_pos, middle_pos = -1, -1
for pos in range(len(string)):
offset = 0
if pos < rightmost_pos:
offset = odd_palindromes[middle_pos - (pos - middle_pos)]
if pos + offset > rightmost_pos:
offset = rightmost_pos - pos
palindrome_len = palindrome_here_odd(pos, offset)
if pos + palindrome_len > rightmost_pos:
rightmost_pos = pos + palindrome_len
middle_pos = pos
odd_palindromes.append(palindrome_len)
even_palindromes = [0]
rightmost_pos, middle_pos = -1, -1
for pos in range(1, len(string)):
offset = 0
if pos < rightmost_pos:
offset = even_palindromes[middle_pos - (pos - middle_pos)]
if pos + offset > rightmost_pos:
offset = rightmost_pos - pos
palindrome_len = palindrome_here_even(pos, offset)
if pos + palindrome_len > rightmost_pos:
rightmost_pos = pos + palindrome_len
middle_pos = pos
even_palindromes.append(palindrome_len)
return [odd_palindromes, even_palindromes]
def minCutWrong(self, s: str) -> int:
if not s:
return 0
odd_palindromes, even_palindromes = self.max_palindromes(s)
# print(s, odd_palindromes, even_palindromes)
max_jump_odd = [0] * len(s)
for pos, length in enumerate(odd_palindromes):
max_jump_odd[pos - (length - 1)] = max(max_jump_odd[pos - (length - 1)], pos + length - 1 + 1)
max_jump_even = [0] * len(s)
for pos, length in enumerate(even_palindromes):
max_jump_even[pos - length] = max(max_jump_even[pos - length], pos + length)
dp = list(range((len(s) + 1)))
for pos in range(len(s)):
left, right = pos, max_jump_even[pos]
while left < right:
dp[right] = min(dp[right], dp[left] + 1)
left += 1
right -= 1
left, right = pos, max_jump_odd[pos]
while left < right:
dp[right] = min(dp[right], dp[left] + 1)
left += 1
right -= 1
print(dp)
print(max_jump_odd, max_jump_even)
return dp[-1] - 1
def minCut(self, s: str) -> int:
if not s:
return 0
palindrome = [[False] * len(s) for _ in s]
def check_palindrome(left: int, right: int) -> None:
while left >= 0 and right < len(s) and s[left] == s[right]:
palindrome[left][right] = True
left -= 1
right += 1
for pos in range(len(s)):
check_palindrome(pos, pos)
check_palindrome(pos, pos + 1)
dp = list(range(len(s) + 1))
for left in range(len(s)):
for right in range(left, len(s)):
if palindrome[left][right]:
dp[right + 1] = min(
dp[right + 1],
dp[left] + 1,
)
return dp[-1] - 1
def minCutRecursive(self, s: str) -> int:
if not s:
return 0
palindrome = [[False] * len(s) for _ in s]
def check_palindrome(left: int, right: int) -> None:
while left >= 0 and right < len(s) and s[left] == s[right]:
palindrome[left][right] = True
left -= 1
right += 1
for pos in range(len(s)):
check_palindrome(pos, pos)
check_palindrome(pos, pos + 1)
@lru_cache(None)
def dfs(pos: int) -> int:
min_lvl = len(s) - pos
left = pos
for right in range(left, len(s)):
if palindrome[left][right]:
min_lvl = min(
min_lvl,
dfs(right + 1) + 1
)
return min_lvl
return dfs(0) - 1
def minCutBruteForce(self, s: str) -> int:
if not s:
return 0
mem = defaultdict(list)
def check_palindrome(left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
mem[left].append(right + 1)
left -= 1
right += 1
for pos in range(len(s)):
check_palindrome(pos, pos)
check_palindrome(pos, pos + 1)
def dfs(start, level=0, level_min=float("+inf")):
if start == len(s) or level == level_min:
return level
for start_next in reversed(mem[start]):
level_min = min(level_min, dfs(start_next, level + 1, level_min))
return level_min
return dfs(0) - 1
class TestSolution:
def setup(self):
self.sol = Solution()
def test_empty(self):
assert self.sol.minCut("") == 0
def test_one(self):
assert self.sol.minCut("a") == 0
def test_case1(self):
assert self.sol.minCut("aba") == 0
def test_case2(self):
assert self.sol.minCut("aaabaaabaac") == 2
def test_case3(self):
assert self.sol.minCut("aab") == 1
def test_case4(self):
assert self.sol.minCut("ababababababababababababcbabababababababababababa") == 0
def test_case5(self):
assert (
self.sol.minCut(
"apjesgpsxoeiokmqmfgvjslcjukbqxpsobyhjpbgdfruqdkeiszrlmtwgfxyfostpqczidfljwfbbrflkgdvtytbgqalguewnhvvmcgxboycffopmtmhtfizxkmeftcucxpobxmelmjtuzigsxnncxpaibgpuijwhankxbplpyejxmrrjgeoevqozwdtgospohznkoyzocjlracchjqnggbfeebmuvbicbvmpuleywrpzwsihivnrwtxcukwplgtobhgxukwrdlszfaiqxwjvrgxnsveedxseeyeykarqnjrtlaliyudpacctzizcftjlunlgnfwcqqxcqikocqffsjyurzwysfjmswvhbrmshjuzsgpwyubtfbnwajuvrfhlccvfwhxfqthkcwhatktymgxostjlztwdxritygbrbibdgkezvzajizxasjnrcjwzdfvdnwwqeyumkamhzoqhnqjfzwzbixclcxqrtniznemxeahfozp"
)
== 452
)
def test_case6(self):
assert (
self.sol.minCut(
"eegiicgaeadbcfacfhifdbiehbgejcaeggcgbahfcajfhjjdgj"
)
== 42
)
def test_case7(self):
assert (
self.sol.minCut(
"adabdcaebdcebdcacaaaadbbcadabcbeabaadcbcaaddebdbddcbdacdbbaedbdaaecabdceddccbdeeddccdaabbabbdedaaabcdadbdabeacbeadbaddcbaacdbabcccbaceedbcccedbeecbccaecadccbdbdccbcbaacccbddcccbaedbacdbcaccdcaadcbaebebcceabbdcdeaabdbabadeaaaaedbdbcebcbddebccacacddebecabccbbdcbecbaeedcdacdcbdbebbacddddaabaedabbaaabaddcdaadcccdeebcabacdadbaacdccbeceddeebbbdbaaaaabaeecccaebdeabddacbedededebdebabdbcbdcbadbeeceecdcdbbdcbdbeeebcdcabdeeacabdeaedebbcaacdadaecbccbededceceabdcabdeabbcdecdedadcaebaababeedcaacdbdacbccdbcece"
)
== 273
)