/
Palindrome Partitioning II.py
53 lines (46 loc) · 1.63 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
class Solution:
# @param s, a string
# @return a list of lists of string
def minCut(self, s):
dp = [[False] * len(s) for i in range(len(s))]
mincut = [i for i in range(len(s))]
for i in range(len(s)):
for j in range(i, -1, -1):
if i == j:
dp[i][j] = True
elif j + 1 == i and s[j] == s[i]:
dp[i][j] = True
elif dp[i - 1][j + 1] and s[j] == s[i]:
dp[i][j] = True
if dp[i][j]:
if j > 0:
mincut[i] = min(mincut[i], mincut[j - 1] + 1)
else:
mincut[i] = 0
return mincut[-1]
class Solution:
# @param {string} s
# @return {integer}
def minCut(self, s):
dpcut = [i for i in range(len(s))]
dpmat = [[False] * len(s) for i in range(len(s))]
for i in range(len(s)):
dpmat[i][i] = True
if i + 1 < len(s) and s[i] == s[i + 1]:
dpmat[i][i + 1] = True
for i in range(len(s) - 3, -1, -1):
for j in range(i + 2, len(s)):
if s[i] == s[j] and dpmat[i + 1][j - 1]:
dpmat[i][j] = True
for i in range(len(s)):
for j in range(i, len(s)):
if dpmat[i][j]:
if i > 0:
dpcut[j] = min(dpcut[j], 1 + dpcut[i - 1])
else:
dpcut[j] = min(dpcut[j], 0)
return dpcut[-1]
if __name__ == "__main__":
a = Solution()
print a.minCut('aab')
print a.minCut('bb')