Skip to content

Commit 3f4903f

Browse files
committed
+ problem 131
1 parent 3d1c076 commit 3f4903f

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 131. Palindrome Partitioning
2+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return *all possible palindrome partitioning of* `s`.
3+
4+
#### Example 1:
5+
<pre>
6+
<strong>Input:</strong> s = "aab"
7+
<strong>Output:</strong> [["a","a","b"],["aa","b"]]
8+
</pre>
9+
10+
#### Example 2:
11+
<pre>
12+
<strong>Input:</strong> s = "a"
13+
<strong>Output:</strong> [["a"]]
14+
</pre>
15+
16+
#### Constraints:
17+
* `1 <= s.length <= 16`
18+
* `s` contains only lowercase English letters.
19+
20+
## Solutions (Python)
21+
22+
### 1. Solution
23+
```Python
24+
from functools import lru_cache
25+
26+
27+
class Solution:
28+
@lru_cache()
29+
def partition(self, s: str) -> List[List[str]]:
30+
if len(s) == 0:
31+
return [[]]
32+
33+
ret = []
34+
35+
for i in range(1, len(s) + 1):
36+
if s[:i] == s[:i][::-1]:
37+
ret.extend([[s[:i]] + x for x in self.partition(s[i:])])
38+
39+
return ret
40+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 131. 分割回文串
2+
给你一个字符串 `s`,请你将 `s` 分割成一些子串,使每个子串都是 **回文串** 。返回 `s` 所有可能的分割方案。
3+
4+
#### 示例 1:
5+
<pre>
6+
<strong>输入:</strong> s = "aab"
7+
<strong>输出:</strong> [["a","a","b"],["aa","b"]]
8+
</pre>
9+
10+
#### 示例 2:
11+
<pre>
12+
<strong>输入:</strong> s = "a"
13+
<strong>输出:</strong> [["a"]]
14+
</pre>
15+
16+
#### 提示:
17+
* `1 <= s.length <= 16`
18+
* `s` 仅由小写英文字母组成
19+
20+
## 题解 (Python)
21+
22+
### 1. 题解
23+
```Python
24+
from functools import lru_cache
25+
26+
27+
class Solution:
28+
@lru_cache()
29+
def partition(self, s: str) -> List[List[str]]:
30+
if len(s) == 0:
31+
return [[]]
32+
33+
ret = []
34+
35+
for i in range(1, len(s) + 1):
36+
if s[:i] == s[:i][::-1]:
37+
ret.extend([[s[:i]] + x for x in self.partition(s[i:])])
38+
39+
return ret
40+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from functools import lru_cache
2+
3+
4+
class Solution:
5+
@lru_cache()
6+
def partition(self, s: str) -> List[List[str]]:
7+
if len(s) == 0:
8+
return [[]]
9+
10+
ret = []
11+
12+
for i in range(1, len(s) + 1):
13+
if s[:i] == s[:i][::-1]:
14+
ret.extend([[s[:i]] + x for x in self.partition(s[i:])])
15+
16+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
[122][122l] |[Best Time to Buy and Sell Stock II][122] |![rs]
109109
[125][125l] |[Valid Palindrome][125] |![py]
110110
[129][129l] |[Sum Root to Leaf Numbers][129] |![py]
111+
[131][131l] |[Palindrome Partitioning][131] |![py]
111112
[133][133l] |[Clone Graph][133] |![py]
112113
[134][134l] |[Gas Station][134] |![rb]&nbsp;&nbsp;![rs]
113114
[136][136l] |[Single Number][136] |![rs]
@@ -1536,6 +1537,7 @@
15361537
[122]:Problemset/0122-Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README.md#122-best-time-to-buy-and-sell-stock-ii
15371538
[125]:Problemset/0125-Valid%20Palindrome/README.md#125-valid-palindrome
15381539
[129]:Problemset/0129-Sum%20Root%20to%20Leaf%20Numbers/README.md#129-sum-root-to-leaf-numbers
1540+
[131]:Problemset/0131-Palindrome%20Partitioning/README.md#131-palindrome-partitioning
15391541
[133]:Problemset/0133-Clone%20Graph/README.md#133-clone-graph
15401542
[134]:Problemset/0134-Gas%20Station/README.md#134-gas-station
15411543
[136]:Problemset/0136-Single%20Number/README.md#136-single-number
@@ -2958,6 +2960,7 @@
29582960
[122l]:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
29592961
[125l]:https://leetcode.com/problems/valid-palindrome/
29602962
[129l]:https://leetcode.com/problems/sum-root-to-leaf-numbers/
2963+
[131l]:https://leetcode.com/problems/palindrome-partitioning/
29612964
[133l]:https://leetcode.com/problems/clone-graph/
29622965
[134l]:https://leetcode.com/problems/gas-station/
29632966
[136l]:https://leetcode.com/problems/single-number/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
[122][122l] |[买卖股票的最佳时机 II][122] |![rs]
109109
[125][125l] |[验证回文串][125] |![py]
110110
[129][129l] |[求根到叶子节点数字之和][129] |![py]
111+
[131][131l] |[分割回文串][131] |![py]
111112
[133][133l] |[克隆图][133] |![py]
112113
[134][134l] |[加油站][134] |![rb]&nbsp;&nbsp;![rs]
113114
[136][136l] |[只出现一次的数字][136] |![rs]
@@ -1536,6 +1537,7 @@
15361537
[122]:Problemset/0122-Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/README_CN.md#122-买卖股票的最佳时机-ii
15371538
[125]:Problemset/0125-Valid%20Palindrome/README_CN.md#125-验证回文串
15381539
[129]:Problemset/0129-Sum%20Root%20to%20Leaf%20Numbers/README_CN.md#129-求根到叶子节点数字之和
1540+
[131]:Problemset/0131-Palindrome%20Partitioning/README_CN.md#131-分割回文串
15391541
[133]:Problemset/0133-Clone%20Graph/README_CN.md#133-克隆图
15401542
[134]:Problemset/0134-Gas%20Station/README_CN.md#134-加油站
15411543
[136]:Problemset/0136-Single%20Number/README_CN.md#136-只出现一次的数字
@@ -2958,6 +2960,7 @@
29582960
[122l]:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
29592961
[125l]:https://leetcode.cn/problems/valid-palindrome/
29602962
[129l]:https://leetcode.cn/problems/sum-root-to-leaf-numbers/
2963+
[131l]:https://leetcode.cn/problems/palindrome-partitioning/
29612964
[133l]:https://leetcode.cn/problems/clone-graph/
29622965
[134l]:https://leetcode.cn/problems/gas-station/
29632966
[136l]:https://leetcode.cn/problems/single-number/

0 commit comments

Comments
 (0)