Skip to content

Commit

Permalink
add 131, 132
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Nov 26, 2015
1 parent 0406cad commit c2034d7
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Backtracking/131_PalindromePartitioning.py
@@ -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")

8 changes: 8 additions & 0 deletions DepthFirstSearch/README.md
@@ -0,0 +1,8 @@
深度优先搜索策略





注意复位当前节点属性。

43 changes: 43 additions & 0 deletions DynamicProgramming/132_PalindromePartitioningII.py
@@ -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")
"""
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -92,6 +92,7 @@
* 052. N-Queens II
* 079. Word Search
* 093. [Restore IP Addresses](Backtracking/93_RestoreIPAddresses.py)
* 131. [Palindrome Partitioning](Backtracking/131_PalindromePartitioning.py)

# [Recursion](Recursion/)

Expand All @@ -116,6 +117,7 @@
* 120. [Triangle](DynamicProgramming/120_Triangle.py)
* 121. [Best Time to Buy and Sell Stock](DynamicProgramming/121_BestTimeToBuyAndSellStock.py)
* 123. [Best Time to Buy and Sell Stock III](DynamicProgramming/123_BestTimeToBuyAndSellStockIII.py)
* 132. [Palindrome Partitioning II](DynamicProgramming/132_PalindromePartitioningII.py)

# [Greedy](Greedy/)

Expand Down
51 changes: 51 additions & 0 deletions ToBeOptimized/131_PalindromePartitioning.py
@@ -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")

43 changes: 43 additions & 0 deletions ToBeOptimized/132_PalindromePartitioningII.py
@@ -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")
"""
44 changes: 44 additions & 0 deletions ToBeOptimized/132_PalindromePartitioningII_Optimized.py
@@ -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")
"""

0 comments on commit c2034d7

Please sign in to comment.