Skip to Content

Palindrome Partitioning II

Home | Coding Interviews | Dynamic Programming | Palindrome Partitioning II

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.

# top-down memo
class Solution:
    def minCut(self, s: str) -> int:
        memo_mincut = dict()
        memo_palindrome = dict()
        
        def is_palindrome(start, end):
            if (start, end) in memo_palindrome:
                return memo_palindrome[(start, end)]
            
            if start >= end:
                return True
            
            memo_palindrome[(start, end)] = (is_palindrome(start + 1, end - 1) and s[start] == s[end])
            return memo_palindrome[(start, end)]
        
        def dfs(idx):
            if is_palindrome(idx, len(s) - 1):
                return 0
            
            if idx in memo_mincut:
                return memo_mincut[idx]
            
            ans = math.inf
            for i in range(idx, len(s)):
                if is_palindrome(idx, i):
                    ans = min(ans, dfs(i + 1) + 1)
            
            memo_mincut[idx] = ans
            return ans

        return dfs(0)

# bottom-up DP
class Solution:
    def minCut(self, s: str) -> int:
        N = len(s)
        dp = [math.inf] * (N + 1)
        dp[0] = -1

        memo_palindrome = dict()

        def is_palindrome(start, end):
            if (start, end) in memo_palindrome:
                return memo_palindrome[(start, end)]

            if start >= end:
                return True
            
            memo_palindrome[(start, end)] = is_palindrome(start + 1, end - 1) and s[start - 1] == s[end - 1]
            return memo_palindrome[(start, end)]
        
        for i in range(1, N + 1):
            for j in range(i, 0, -1):
                if is_palindrome(j, i):
                    dp[i] = min(dp[i], dp[j - 1] + 1)
        
        return dp[N]

# two-pointer optimised (i.e. expand from centre)
class Solution:
    def minCut(self, s: str) -> int:
        N = len(s)
        dp = [math.inf] * N

        def optimal_sub_problem(left, right, dp):
            while 0 <= left  and  right < len(s) and s[left] == s[right]:
                if left == 0:
                    dp[right] = 0

                dp[right] = min(dp[right], dp[left - 1] + 1)
                left -= 1
                right += 1
        
        for i in range(N):
            optimal_sub_problem(i, i, dp)
            optimal_sub_problem(i - 1, i, dp)

        return dp[N - 1]

Posted by Jamie Meyer 3 months ago

Related Problems

Given an integer array nums, return the length of the longest strictly increasing subsequence.

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

Return the maximum coins you can collect by bursting the balloons wisely.

The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).

To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

Return the knight's minimum initial health so that he can rescue the princess.

Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.