##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1278.html

# 1278. Palindrome Partitioning III (Hard)

You are given a string s containing lowercase letters and an integer k. You need to :

• First, change some characters of s to other lowercase English letters.
• Then divide s into k non-empty disjoint substrings such that each substring is palindrome.

Return the minimal number of characters that you need to change to divide the string.

Example 1:

Input: s = "abc", k = 2
Output: 1
Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.


Example 2:

Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.

Example 3:

Input: s = "leetcode", k = 8
Output: 0


Constraints:

• 1 <= k <= s.length <= 100.
• s only contains lowercase English letters.

Related Topics:
Dynamic Programming

## Solution 1. DP

Let dp[k][i][j] be the minimal number of changes needed for s[i..j] with k divides.

dp[1][i][i] = 0
dp[1][i][j] = dp[1][i + 1][j - 1]       If s[i] == s[j]
= 1 + dp[1][i + 1][j - 1]   If s[i] != s[j]

dp[k][i][j] = min( dp[k-1][i][t] + dp[1][t+1][j] | i + k - 1 <= t < j )

// OJ: https://leetcode.com/problems/palindrome-partitioning-iii/
// Time: O(K * N^3)
// Space: O(K * N^3)
class Solution {
typedef long long LL;
inline void setMin(LL &a, LL b) { a = min(a, b); }
public:
int palindromePartition(string s, int K) {
int N = s.size();
vector<vector<vector<LL>>> dp(K + 1, vector<vector<LL>>(N, vector<LL>(N, 1e9)));
for (int i = 0; i < N; ++i) dp[1][i][i] = 0;
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
dp[1][i][j] = (s[i] != s[j]) + (i + 1 <= j - 1 ? dp[1][i + 1][j - 1] : 0);
}
}
for (int k = 2; k <= K; ++k) {
for (int i = N - 2; i >= 0; --i) {
for (int j = i + k - 1; j < N; ++j) {
for (int t = i + k - 2; t < j; ++t) {
setMin(dp[k][i][j], dp[k - 1][i][t] + dp[1][t + 1][j]);
}
}
}
}
return dp[K][0][N - 1];
}
};


## Solution 2. DP

In Solution 1, for the k >= 2 cases, we don’t need to iterate all i, j combinations.

Let pal[i][j] be the minimum changes needed to make s[i..j] palindrome.

Let dp[k][i] be the minimum changes needed for s[0..i] with k divides.

pal[i][i] = 0
pal[i][j] = dp[1][i + 1][j - 1]       If s[i] == s[j]
= 1 + dp[1][i + 1][j - 1]   If s[i] != s[j]

dp[1][i] = pal[0][i]
dp[k][i] = min( dp[k-1][j] + pal[j+1][i] | k-2 <= j < i )

// OJ: https://leetcode.com/problems/palindrome-partitioning-iii/
// Time: O(K * N^2)
// Space: O(K * N^2)
class Solution {
typedef long long LL;
inline void setMin(LL &a, LL b) { a = min(a, b); }
public:
int palindromePartition(string s, int K) {
int N = s.size();
vector<vector<LL>> pal(N, vector<LL>(N));
vector<vector<LL>> dp(K + 1, vector<LL>(N, 1e9));
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
pal[i][j] = (s[i] != s[j]) + pal[i + 1][j - 1];
}
}
for (int i = 0; i < N; ++i) dp[1][i] = pal[0][i];
for (int k = 2; k <= K; ++k) {
for (int i = k - 1; i < N; ++i) {
for (int j = k - 2; j < i; ++j) {
setMin(dp[k][i], dp[k - 1][j] + pal[j + 1][i]);
}
}
}
return dp[K][N - 1];
}
};

• class Solution {
public int palindromePartition(String s, int k) {
int length = s.length();
int[][] costs = new int[length][length];
for (int i = 1; i < length; i++) {
if (s.charAt(i - 1) != s.charAt(i))
costs[i - 1][i] = 1;
}
for (int i = length - 3; i >= 0; i--) {
for (int j = i + 2; j < length; j++) {
costs[i][j] = costs[i + 1][j - 1];
if (s.charAt(i) != s.charAt(j))
costs[i][j]++;
}
}
int[][] dp = new int[length + 1][k + 1];
for (int i = 0; i <= length; i++) {
for (int j = 0; j <= k; j++)
dp[i][j] = Integer.MAX_VALUE;
}
dp[0][0] = 0;
for (int i = 1; i <= length; i++) {
int max = Math.min(i, k);
for (int j = 1; j <= max; j++) {
if (j == 1)
dp[i][j] = costs[0][i - 1];
else {
for (int start = j - 1; start < i; start++)
dp[i][j] = Math.min(dp[i][j], dp[start][j - 1] + costs[start][i - 1]);
}
}
}
return dp[length][k];
}
}

############

class Solution {
public int palindromePartition(String s, int k) {
int n = s.length();
int[][] g = new int[n][n];
for (int i = n - 1; i >= 0; --i) {
for (int j = i; j < n; ++j) {
g[i][j] = s.charAt(i) != s.charAt(j) ? 1 : 0;
if (i + 1 < j) {
g[i][j] += g[i + 1][j - 1];
}
}
}
int[][] f = new int[n + 1][k + 1];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= Math.min(i, k); ++j) {
if (j == 1) {
f[i][j] = g[0][i - 1];
} else {
f[i][j] = 10000;
for (int h = j - 1; h < i; ++h) {
f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]);
}
}
}
}
return f[n][k];
}
}

• // OJ: https://leetcode.com/problems/palindrome-partitioning-iii/
// Time: O(K * N^3)
// Space: O(K * N^3)
class Solution {
typedef long long LL;
inline void setMin(LL &a, LL b) { a = min(a, b); }
public:
int palindromePartition(string s, int K) {
int N = s.size();
vector<vector<vector<LL>>> dp(K + 1, vector<vector<LL>>(N, vector<LL>(N, 1e9)));
for (int i = 0; i < N; ++i) dp[1][i][i] = 0;
for (int i = N - 2; i >= 0; --i) {
for (int j = i + 1; j < N; ++j) {
dp[1][i][j] = (s[i] != s[j]) + (i + 1 <= j - 1 ? dp[1][i + 1][j - 1] : 0);
}
}
for (int k = 2; k <= K; ++k) {
for (int i = N - 2; i >= 0; --i) {
for (int j = i + k - 1; j < N; ++j) {
for (int t = i + k - 2; t < j; ++t) {
setMin(dp[k][i][j], dp[k - 1][i][t] + dp[1][t + 1][j]);
}
}
}
}
return dp[K][0][N - 1];
}
};

• class Solution:
def palindromePartition(self, s: str, k: int) -> int:
n = len(s)
g = [[0] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
g[i][j] = int(s[i] != s[j])
if i + 1 < j:
g[i][j] += g[i + 1][j - 1]

f = [[0] * (k + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, min(i, k) + 1):
if j == 1:
f[i][j] = g[0][i - 1]
else:
f[i][j] = inf
for h in range(j - 1, i):
f[i][j] = min(f[i][j], f[h][j - 1] + g[h][i - 1])
return f[n][k]


• func palindromePartition(s string, k int) int {
n := len(s)
g := make([][]int, n)
for i := range g {
g[i] = make([]int, n)
}
for i := n - 1; i >= 0; i-- {
for j := 1; j < n; j++ {
if s[i] != s[j] {
g[i][j] = 1
}
if i+1 < j {
g[i][j] += g[i+1][j-1]
}
}
}
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, k+1)
}
for i := 1; i <= n; i++ {
for j := 1; j <= min(i, k); j++ {
if j == 1 {
f[i][j] = g[0][i-1]
} else {
f[i][j] = 100000
for h := j - 1; h < i; h++ {
f[i][j] = min(f[i][j], f[h][j-1]+g[h][i-1])
}
}
}
}
return f[n][k]
}

func min(a, b int) int {
if a < b {
return a
}
return b
}