Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class Solution(object): | ||
def partition(self, s): | ||
if not s: | ||
return [] | ||
self.result = [] | ||
self.end = len(s) | ||
self.str = s | ||
|
||
self.is_palindrome = [[False for i in range(self.end)] | ||
for j in range(self.end)] | ||
|
||
for i in range(self.end-1, -1, -1): | ||
for j in range(self.end): | ||
if i > j: | ||
pass | ||
elif j-i < 2 and s[i] == s[j]: | ||
self.is_palindrome[i][j] = True | ||
elif self.is_palindrome[i+1][j-1] and s[i] == s[j]: | ||
self.is_palindrome[i][j] = True | ||
else: | ||
self.is_palindrome[i][j] = False | ||
|
||
self.palindrome_partition(0, []) | ||
return self.result | ||
|
||
def palindrome_partition(self, start, sub_strs): | ||
if start == self.end: | ||
# It's confused the following sentence doesn't work. | ||
# self.result.append(sub_strs) | ||
self.result.append(sub_strs[:]) | ||
return | ||
|
||
for i in range(start, self.end): | ||
if self.is_palindrome[start][i]: | ||
sub_strs.append(self.str[start:i+1]) | ||
self.palindrome_partition(i+1, sub_strs) | ||
sub_strs.pop() # Backtracking here | ||
|
||
|
||
if __name__ == "__main__": | ||
sol = Solution() | ||
print sol.partition("aab") | ||
print sol.partition("aabb") | ||
print sol.partition("aabaa") | ||
print sol.partition("acbca") | ||
print sol.partition("acbbca") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
深度优先搜索策略 | ||
|
||
|
||
|
||
|
||
|
||
注意复位当前节点属性。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class Solution(object): | ||
""" | ||
Dynamic Programming: | ||
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:] | ||
is_palindrome[i][j]: whether s[i:i+1] is palindrome | ||
""" | ||
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] | ||
|
||
""" | ||
if __name__ == "__main__": | ||
sol = Solution() | ||
print sol.minCut("aab") | ||
print sol.minCut("aabb") | ||
print sol.minCut("aabaa") | ||
print sol.minCut("acbca") | ||
print sol.minCut("acbbca") | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class Solution(object): | ||
def partition(self, s): | ||
if not s: | ||
return [] | ||
self.result = [] | ||
self.end = len(s) | ||
self.str = s | ||
|
||
self.is_palindrome = [[False for i in range(self.end)] | ||
for j in range(self.end)] | ||
|
||
for i in range(self.end-1, -1, -1): | ||
for j in range(self.end): | ||
if i > j: | ||
pass | ||
elif j-i < 2 and s[i] == s[j]: | ||
self.is_palindrome[i][j] = True | ||
elif self.is_palindrome[i+1][j-1] and s[i] == s[j]: | ||
self.is_palindrome[i][j] = True | ||
else: | ||
self.is_palindrome[i][j] = False | ||
|
||
self.palindrome_partition(0, []) | ||
return self.result | ||
|
||
def palindrome_partition(self, start, sub_strs): | ||
if start == self.end: | ||
# It's confused the following sentence doesn't work. | ||
# self.result.append(sub_strs) | ||
self.result.append(sub_strs[:]) | ||
return | ||
|
||
for i in range(start, self.end): | ||
if self.is_palindrome[start][i]: | ||
sub_strs.append(self.str[start:i+1]) | ||
self.palindrome_partition(i+1, sub_strs) | ||
sub_strs.pop() # Backtracking here | ||
|
||
|
||
if __name__ == "__main__": | ||
sol = Solution() | ||
print sol.partition("aab") | ||
print sol.partition("aabb") | ||
print sol.partition("aabaa") | ||
print sol.partition("acbca") | ||
print sol.partition("acbbca") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class Solution(object): | ||
""" | ||
Dynamic Programming: | ||
cuts[i]: minimum cuts needed for a palindrome partitioning of s[i:] | ||
is_palindrome[i][j]: whether s[i:i+1] is palindrome | ||
""" | ||
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] | ||
|
||
""" | ||
if __name__ == "__main__": | ||
sol = Solution() | ||
print sol.minCut("aab") | ||
print sol.minCut("aabb") | ||
print sol.minCut("aabaa") | ||
print sol.minCut("acbca") | ||
print sol.minCut("acbbca") | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Refer to: https://leetcode.com/discuss/9476/solution-does-not-need-table-palindrome-right-uses-only-space | ||
# A better solution | ||
|
||
|
||
class Solution(object): | ||
""" | ||
Dynamic Programming: | ||
""" | ||
def minCut(self, s): | ||
s_len = len(s) | ||
# number of minnum cuts for the pre i characters | ||
min_cuts = [i-1 for i in range(s_len+1)] | ||
|
||
for i in range(s_len): | ||
# odd length palindrome | ||
j = 0 | ||
while i-j >= 0 and i+j < s_len: | ||
if s[i-j] == s[i+j]: | ||
min_cuts[i+j+1] = min(min_cuts[i+j+1], min_cuts[i-j]+1) | ||
j += 1 | ||
else: | ||
break | ||
# even length palindrome | ||
j = 1 | ||
while i-j+1 >= 0 and i+j < s_len: | ||
if s[i-j+1] == s[i+j]: | ||
min_cuts[i+j+1] = min(min_cuts[i+j+1], min_cuts[i-j+1]+1) | ||
j += 1 | ||
else: | ||
break | ||
|
||
return min_cuts[s_len] | ||
|
||
""" | ||
if __name__ == "__main__": | ||
sol = Solution() | ||
print sol.minCut("aab") | ||
print sol.minCut("aabb") | ||
print sol.minCut("aabaa") | ||
print sol.minCut("acbca") | ||
print sol.minCut("acbbca") | ||
""" |