-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path131.py
More file actions
24 lines (22 loc) · 674 Bytes
/
Copy path131.py
File metadata and controls
24 lines (22 loc) · 674 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
class Solution:
def partition(self, s: str) -> List[List[str]]:
ret = []
self.Backtrack(s, 0, [], ret)
return ret
def Backtrack(self, s, k, curr, ret):
if k == len(s):
ret.append([c for c in curr])
return curr
for i in range(k, len(s)):
if self.IsPalindrome(s, k, i):
curr.append(s[k:i + 1])
curr = self.Backtrack(s, i + 1, curr, ret)
curr.pop()
return curr
def IsPalindrome(self, s, i, j):
while j > i:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True