132. Palindrome Partitioning II (Hard)

Description:

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Example:

Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.


Solutions:

Solution1:

class Solution:
    def minCut(self, string: str) -> int:
        if not string:
            return 0
        
        l = len(string)
        archive = [[] for i in range(l)]
        
        for i in range(l):
            expand = 0
            while i-expand > -1 and i+expand < l and string[i-expand] == string[i+expand]:
                archive[i-expand].append(i+expand+1)
                expand += 1
            
            if i+1 < l and string[i] == string[i+1]:
                expand = 1
                while i-expand+1 > -1 and i+expand < l and string[i-expand+1] == string[i+expand]:
                    archive[i-expand+1].append(i+expand+1)
                    expand += 1
        
        dic = {}
        
        def helper(left):
            if left == l:
                return 0
            
            if left not in dic:
                dic[left] = 1 + min([helper(next_) for next_ in archive[left]])
            return dic[left]
    
        return helper(0)-1

Runtime: 400 ms, faster than 74.29% of Python3 online submissions for Palindrome Partitioning II.
Memory Usage: 40.3 MB, less than 5.37% of Python3 online submissions for Palindrome Partitioning II.

网上看了一圈Python解法都在450ms以上,然而发现竟然有sample 36 ms submission,结果其实这个解法的重点在于#acceleration部分。把acceleration部分加到我的代码里,也成了40ms。

class Solution:
    def minCut(self, string: str) -> int:
        # acceleration
        if string == string[::-1]: return 0
        for i in range(1, len(string)):
            if string[:i] == string[:i][::-1] and string[i:] == string[i:][::-1]:
                return 1
        
        l = len(string)
        archive = [[] for i in range(l)]
        
        for i in range(l):
            expand = 0
            while i-expand > -1 and i+expand < l and string[i-expand] == string[i+expand]:
                archive[i-expand].append(i+expand+1)
                expand += 1
            
            if i+1 < l and string[i] == string[i+1]:
                expand = 1
                while i-expand+1 > -1 and i+expand < l and string[i-expand+1] == string[i+expand]:
                    archive[i-expand+1].append(i+expand+1)
                    expand += 1
        
        dic = {}
        
        def helper(left):
            if left == l:
                return 0
            
            if left not in dic:
                dic[left] = 1 + min([helper(next_) for next_ in archive[left]])
            return dic[left]
    
        return helper(0)-1

Runtime: 40 ms, faster than 94.29% of Python3 online submissions for Palindrome Partitioning II.
Memory Usage: 15.1 MB, less than 43.63% of Python3 online submissions for Palindrome Partitioning II.

你可能感兴趣的:(132. Palindrome Partitioning II (Hard))