-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path132_PalindromePartitioningII.py
executable file
·56 lines (45 loc) · 1.78 KB
/
132_PalindromePartitioningII.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
"""
Dynamic Programming:
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:], so we want cuts[0].
To get cuts[i-1], we scan j from i-1 to len(s)-1.
Once we comes to a is_palindrome[i-1][j]==true:
if j==len(s)-1, the string s[i-1:] is a Pal, cuts[i-1] is 0;
else: the current cut num (first cut s[i-1:j+1] and then cut the rest
s[j+1:]) is 1+cuts[j+1], compare it to the exisiting cuts[i-1], repalce if smaller.
is_palindrome[i][j]: whether s[i:j+1] is palindrome.
Here we need not to compute the is_palindrome in advance.
We use "Dynamic Programming" too, the formula is very intuitive:
is_palindrome[i][j] = true if (is_palindrome[i+1][j-1] and s[i] == s[j]) else false
A better O(n) space solution can be found here:
https://discuss.leetcode.com/topic/2840/my-solution-does-not-need-a-table-for-palindrome-is-it-right-it-uses-only-o-n-space
"""
def minCut(self, s):
if not s:
return 0
s_len = len(s)
is_palindrome = [[False for i in range(s_len)]
for j in range(s_len)]
cuts = [s_len - 1 - i for i in range(s_len)]
for i in range(s_len - 1, -1, -1):
for j in range(i, s_len):
# if self.is_palindrome(i, j):
if ((j - i < 2 and s[i] == s[j]) or (s[i] == s[j] and is_palindrome[i + 1][j - 1])):
is_palindrome[i][j] = True
if j == s_len - 1:
cuts[i] = 0
else:
cuts[i] = min(cuts[i], 1 + cuts[j + 1])
else:
pass
return cuts[0]
"""
""
"aab"
"aabb"
"aabaa"
"acbca"
"acbbca"
"""