-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00131-palindrome_partitioning.py
49 lines (33 loc) · 1.01 KB
/
00131-palindrome_partitioning.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 131: Palindrome Partitioning
# https://leetcode.com/problems/palindrome-partitioning
from typing import List
class Solution:
# SOLUTION
def partition(self, s: str) -> List[List[str]]:
result = []
current = []
def isPalindrome(s: str, left: int, right: int) -> bool:
while (left < right):
if (s[left] != s[right]): return False
left+=1
right-=1
return True
def backtrack(start: int) -> None:
if (start == len(s)):
result.append(current.copy())
return
for i in range(start , len(s)):
if isPalindrome(s, start, i):
str = s[start:i+1]
current.append(str)
backtrack(i+1)
current.pop()
backtrack(0)
return result
if __name__ == "__main__":
o = Solution()
# INPUT
s = "aab"
# OUTPUT
result = o.partition(s)
print(result)