# Leetcode 1278. Palindrome Partitioning III

proheart

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.

1. `s`中的部分字符修改为其他的小写英文字母。
2. `s`分割成`k`个非空且不相交的子串，且每个子串都是回文。

``````f(r, t) = min { f(i, t − 1) + check(i + 1, r) }

`check`函数实现也非常容易，我们可以先定义两个指针`l``r`分别指向字符串的首尾，然后将左右指针向中间靠拢，判断左右指针指向的字符是不是相同，不相同的话就需要改变一次。

• check函数就可以看成切割钢条的price表.只不过price表是直接给出的，check函数需要自己实现

``````// 朴素递归
public int palindromePartition1(String s, int k) {
return dfs(s, s.length() - 1, k);
}

private int dfs(String s, int r, int t) {
if (t == 1) return count(s,0, r);
int res = Integer.MAX_VALUE;
for (int i = 0; i <= r; i++) {
res = Math.min(res, dfs(s, i, t - 1) + count(s, i + 1, r));
}
return res;
}
private int count(String s, int left, int right) {
int c = 0;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) c++;
}
return c;
}``````

``````// 带memo的递归
public int palindromePartition2(String s, int k) {
Integer[][] memo = new Integer[s.length() + 1][k + 1];
return dfsWithMemo(s, s.length() - 1, k, memo);
}

private int dfsWithMemo(String s, int r, int t, Integer[][] memo) {
if (t == 1) return count(s, 0, r);
if (memo[r][t] != null) return memo[r][t];
int res = Integer.MAX_VALUE;
for (int i = 0; i <= r; i++) {
res = Math.min(res, dfsWithMemo(s, i, t - 1, memo) + count(s, i + 1, r));
}
memo[r][t] = res;
return res;
}``````
https://coordinate.wang/index.php/archives/2791/

##### 来刷算法
Learn data structure and algorithm using java.
##### proheart

Developer, Java & Android

38 声望
20 粉丝
0 条评论