-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_131.py
More file actions
29 lines (22 loc) · 766 Bytes
/
LC_131.py
File metadata and controls
29 lines (22 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def partition(self, s: str) -> List[List[str]]:
result = []
@cache
def is_palindrome(x, y):
for i in range(y-x):
if s[x+i] != s[y-i-1]:
return False
return True
@cache
def check(idx):
if idx == len(s):
return [[]]
ret = []
for i in range(idx, len(s)):
substr = s[idx:i+1]
if is_palindrome(idx, i + 1):
palindrome = check(i+1)
for p in palindrome:
ret.append([substr] + p)
return ret
return check(0)