forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0132.py
26 lines (21 loc) · 761 Bytes
/
0132.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
class Solution:
def minCut(self, s):
"""
:type s: str
:rtype: int
"""
if s == s[::-1]:
return 0
for i in range(1, len(s)):
if s[:i] == s[:i][::-1] and s[i:] == s[i:][::-1]:
return 1
mem = list(range(-1,len(s)))
for i in range(len(s)):
r1,r2 = 0,0
while i-r1>=0 and i+r1<len(s) and s[i-r1] == s[i+r1]:
mem[i+r1+1] = min(mem[i+r1+1],mem[i-r1]+1)
r1 += 1
while i-r2>=0 and i+r2+1<len(s) and s[i-r2] == s[i+r2+1]:
mem[i+r2+2] = min(mem[i+r2+2],mem[i-r2]+1)
r2 += 1
return mem[-1]