-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132. 分割回文串 II.py
49 lines (43 loc) · 2.46 KB
/
132. 分割回文串 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
# -*- encoding: utf-8 -*-
'''
@File : 132. 分割回文串 II.py
@Time : 2020/05/02 11:53:21
@Author : windmzx
@Version : 1.0
@Desc : For leetcode template
'''
# here put the import lib
from typing import List
class Solution(object):
def minCut(self, s):
"""
:type s: str
:rtype: int
"""
if s is None or s == "":
return 0
if s==s[::-1]:
return 0
dp = [0 for _ in range(len(s)+1)]
dp[0] = -1
for i in range(1,len(s)+1):
if self.isvaild(s[:i]):
continue
dp[i] = i
for j in range(0, i):
if self.isvaild(s[j:i]):
dp[i] = min(dp[i], dp[j]+1)
print(dp)
return dp[-1]
def isvaild(self, s):
i = 0
j = len(s)-1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
if __name__ == "__main__":
x = Solution()
print(x.minCut("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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