-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path131-Palindrome-Partitioning.py
More file actions
35 lines (29 loc) · 915 Bytes
/
131-Palindrome-Partitioning.py
File metadata and controls
35 lines (29 loc) · 915 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
30
31
32
33
34
35
'''Leetcode- https://leetcode.com/problems/palindrome-partitioning/ '''
'''
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
'''
def partition(s):
res, part = [], []
def dfs(i):
if i >= len(s):
res.append(part.copy())
return
for j in range(i, len(s)):
if isPali(s, i, j):
part.append(s[i:j+1])
dfs(j + 1)
part.pop()
dfs(0)
return res
def isPali( s, l, r):
while l < r:
if s[l] != s[r]:
return False
l, r = l + 1, r - 1
return True
#T :O(2^n)
#S :O(n)