Skip to content

Commit 81477bb

Browse files
committed
+ problem 1278
1 parent fff46d0 commit 81477bb

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 1278. Palindrome Partitioning III
2+
You are given a string `s` containing lowercase letters and an integer `k`. You need to :
3+
* First, change some characters of `s` to other lowercase English letters.
4+
* Then divide `s` into `k` non-empty disjoint substrings such that each substring is a palindrome.
5+
6+
Return *the minimal number of characters that you need to change to divide the string*.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> s = "abc", k = 2
11+
<strong>Output:</strong> 1
12+
<strong>Explanation:</strong> You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> s = "aabbc", k = 3
18+
<strong>Output:</strong> 0
19+
<strong>Explanation:</strong> You can split the string into "aa", "bb" and "c", all of them are palindrome.
20+
</pre>
21+
22+
#### Example 3:
23+
<pre>
24+
<strong>Input:</strong> s = "leetcode", k = 8
25+
<strong>Output:</strong> 0
26+
</pre>
27+
28+
#### Constraints:
29+
* `1 <= k <= s.length <= 100`.
30+
* `s` only contains lowercase English letters.
31+
32+
## Solutions (Python)
33+
34+
### 1. Solution
35+
```Python
36+
from functools import cache
37+
38+
39+
class Solution:
40+
def palindromePartition(self, s: str, k: int) -> int:
41+
@cache
42+
def palindromeChange(i: int, j: int) -> int:
43+
if i >= j:
44+
return 0
45+
46+
return palindromeChange(i + 1, j - 1) + (s[i] != s[j])
47+
48+
@cache
49+
def subPartition(i: int, k: int) -> int:
50+
if k == 1:
51+
return palindromeChange(i, len(s) - 1)
52+
53+
return min(palindromeChange(i, j) + subPartition(j + 1, k - 1) for j in range(i, len(s) - k + 1))
54+
55+
return subPartition(0, k)
56+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 1278. 分割回文串 III
2+
给你一个由小写字母组成的字符串 `s`,和一个整数 `k`
3+
4+
请你按下面的要求分割字符串:
5+
* 首先,你可以将 `s` 中的部分字符修改为其他的小写英文字母。
6+
* 接着,你需要把 `s` 分割成 `k` 个非空且不相交的子串,并且每个子串都是回文串。
7+
8+
请返回以这种方式分割字符串所需修改的最少字符数。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> s = "abc", k = 2
13+
<strong>输出:</strong> 1
14+
<strong>解释:</strong> 你可以把字符串分割成 "ab" 和 "c",并修改 "ab" 中的 1 个字符,将它变成回文串。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> s = "aabbc", k = 3
20+
<strong>输出:</strong> 0
21+
<strong>解释:</strong> 你可以把字符串分割成 "aa"、"bb" 和 "c",它们都是回文串。
22+
</pre>
23+
24+
#### 示例 3:
25+
<pre>
26+
<strong>输入:</strong> s = "leetcode", k = 8
27+
<strong>输出:</strong> 0
28+
</pre>
29+
30+
#### 提示:
31+
* `1 <= k <= s.length <= 100`
32+
* `s` 中只含有小写英文字母。
33+
34+
## 题解 (Python)
35+
36+
### 1. 题解
37+
```Python
38+
from functools import cache
39+
40+
41+
class Solution:
42+
def palindromePartition(self, s: str, k: int) -> int:
43+
@cache
44+
def palindromeChange(i: int, j: int) -> int:
45+
if i >= j:
46+
return 0
47+
48+
return palindromeChange(i + 1, j - 1) + (s[i] != s[j])
49+
50+
@cache
51+
def subPartition(i: int, k: int) -> int:
52+
if k == 1:
53+
return palindromeChange(i, len(s) - 1)
54+
55+
return min(palindromeChange(i, j) + subPartition(j + 1, k - 1) for j in range(i, len(s) - k + 1))
56+
57+
return subPartition(0, k)
58+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def palindromePartition(self, s: str, k: int) -> int:
6+
@cache
7+
def palindromeChange(i: int, j: int) -> int:
8+
if i >= j:
9+
return 0
10+
11+
return palindromeChange(i + 1, j - 1) + (s[i] != s[j])
12+
13+
@cache
14+
def subPartition(i: int, k: int) -> int:
15+
if k == 1:
16+
return palindromeChange(i, len(s) - 1)
17+
18+
return min(palindromeChange(i, j) + subPartition(j + 1, k - 1) for j in range(i, len(s) - k + 1))
19+
20+
return subPartition(0, k)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@
839839
[1275][1275l]|[Find Winner on a Tic Tac Toe Game][1275] |![rs]
840840
[1276][1276l]|[Number of Burgers with No Waste of Ingredients][1276] |![py]&nbsp;&nbsp;![rs]
841841
[1277][1277l]|[Count Square Submatrices with All Ones][1277] |![rs]
842+
[1278][1278l]|[Palindrome Partitioning III][1278] |![py]
842843
[1281][1281l]|[Subtract the Product and Sum of Digits of an Integer][1281] |![rs]
843844
[1282][1282l]|[Group the People Given the Group Size They Belong To][1282] |![rs]
844845
[1283][1283l]|[Find the Smallest Divisor Given a Threshold][1283] |![py]
@@ -2501,6 +2502,7 @@
25012502
[1275]:Problemset/1275-Find%20Winner%20on%20a%20Tic%20Tac%20Toe%20Game/README.md#1275-find-winner-on-a-tic-tac-toe-game
25022503
[1276]:Problemset/1276-Number%20of%20Burgers%20with%20No%20Waste%20of%20Ingredients/README.md#1276-number-of-burgers-with-no-waste-of-ingredients
25032504
[1277]:Problemset/1277-Count%20Square%20Submatrices%20with%20All%20Ones/README.md#1277-count-square-submatrices-with-all-ones
2505+
[1278]:Problemset/1278-Palindrome%20Partitioning%20III/README.md#1278-palindrome-partitioning-iii
25042506
[1281]:Problemset/1281-Subtract%20the%20Product%20and%20Sum%20of%20Digits%20of%20an%20Integer/README.md#1281-subtract-the-product-and-sum-of-digits-of-an-integer
25052507
[1282]:Problemset/1282-Group%20the%20People%20Given%20the%20Group%20Size%20They%20Belong%20To/README.md#1282-group-the-people-given-the-group-size-they-belong-to
25062508
[1283]:Problemset/1283-Find%20the%20Smallest%20Divisor%20Given%20a%20Threshold/README.md#1283-find-the-smallest-divisor-given-a-threshold
@@ -4157,6 +4159,7 @@
41574159
[1275l]:https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/
41584160
[1276l]:https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/
41594161
[1277l]:https://leetcode.com/problems/count-square-submatrices-with-all-ones/
4162+
[1278l]:https://leetcode.com/problems/palindrome-partitioning-iii/
41604163
[1281l]:https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
41614164
[1282l]:https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/
41624165
[1283l]:https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@
839839
[1275][1275l]|[找出井字棋的获胜者][1275] |![rs]
840840
[1276][1276l]|[不浪费原料的汉堡制作方案][1276] |![py]&nbsp;&nbsp;![rs]
841841
[1277][1277l]|[统计全为 1 的正方形子矩阵][1277] |![rs]
842+
[1278][1278l]|[分割回文串 III][1278] |![py]
842843
[1281][1281l]|[整数的各位积和之差][1281] |![rs]
843844
[1282][1282l]|[用户分组][1282] |![rs]
844845
[1283][1283l]|[使结果不超过阈值的最小除数][1283] |![py]
@@ -2501,6 +2502,7 @@
25012502
[1275]:Problemset/1275-Find%20Winner%20on%20a%20Tic%20Tac%20Toe%20Game/README_CN.md#1275-找出井字棋的获胜者
25022503
[1276]:Problemset/1276-Number%20of%20Burgers%20with%20No%20Waste%20of%20Ingredients/README_CN.md#1276-不浪费原料的汉堡制作方案
25032504
[1277]:Problemset/1277-Count%20Square%20Submatrices%20with%20All%20Ones/README_CN.md#1277-统计全为-1-的正方形子矩阵
2505+
[1278]:Problemset/1278-Palindrome%20Partitioning%20III/README_CN.md#1278-分割回文串-iii
25042506
[1281]:Problemset/1281-Subtract%20the%20Product%20and%20Sum%20of%20Digits%20of%20an%20Integer/README_CN.md#1281-整数的各位积和之差
25052507
[1282]:Problemset/1282-Group%20the%20People%20Given%20the%20Group%20Size%20They%20Belong%20To/README_CN.md#1282-用户分组
25062508
[1283]:Problemset/1283-Find%20the%20Smallest%20Divisor%20Given%20a%20Threshold/README_CN.md#1283-使结果不超过阈值的最小除数
@@ -4157,6 +4159,7 @@
41574159
[1275l]:https://leetcode.cn/problems/find-winner-on-a-tic-tac-toe-game/
41584160
[1276l]:https://leetcode.cn/problems/number-of-burgers-with-no-waste-of-ingredients/
41594161
[1277l]:https://leetcode.cn/problems/count-square-submatrices-with-all-ones/
4162+
[1278l]:https://leetcode.cn/problems/palindrome-partitioning-iii/
41604163
[1281l]:https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
41614164
[1282l]:https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/
41624165
[1283l]:https://leetcode.cn/problems/find-the-smallest-divisor-given-a-threshold/

0 commit comments

Comments
 (0)