-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.py
More file actions
28 lines (22 loc) · 721 Bytes
/
13.py
File metadata and controls
28 lines (22 loc) · 721 Bytes
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
class Solution:
def minCut(self, s: str) -> int:
n = len(s)
pal = [[False]*n for _ in range(n)]
for i in range(n-1, -1, -1):
for j in range(i, n):
if s[i] == s[j] and (j-i < 2 or pal[i+1][j-1]):
pal[i][j] = True
def helper(ind):
if ind >= n:
return 0
if dp[ind] != -1:
return dp[ind]
string = ''
cost = float('inf')
for i in range(ind, n):
if pal[ind][i]:
cost = min(cost, 1 + helper(i + 1))
dp[ind] = cost
return cost
dp = [-1]*n
return helper(0) - 1