-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktracking_131_palindromePartitioning.py
55 lines (44 loc) · 1.87 KB
/
backtracking_131_palindromePartitioning.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
50
51
52
53
54
55
"""
https://leetcode.com/problems/palindrome-partitioning/
https://www.youtube.com/watch?v=3jvWodd7ht0&list=PLot-Xpze53lfOdF3KwpMSFEyfE77zIwiP&index=28
leetcode 131
medium
backtracking
input : string s
output: partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Logic : O(2^n)
approach : bruteforce backtracking
-1st partition : each char by itself
-partition, check if palindrome, add to result if yes
-decision tree :
-number of branches = length of string
-1st branch : single char
-2nd branch : 1st 2 chars
-3rd branch : 1st 3 chars
-at every node : check if pali
-if not, stop that branch
-also stop if leaf node reached and pali : add to result
Time Complexity: O(2^n)
"""
def partition(s):
res, part = [], [] #result to store all partitions, part = current partition
def dfs(i): #i : index of current char, recursive func
if i >= len(s): #if out of bounds, index >= length of string :
res.append(part.copy()) #since partition var will be overwritten in another branch
return #base case
for j in range(i, len(s)): #iterate thru each char till end of string reached
if isPali(s, i, j): #check if this substring is a pali or not
part.append(s[i : j + 1]) #if yes, then add this substring to current partition
dfs(j + 1) #j+1: next char, so recursively perform dfs to find additional pali
part.pop() #cleanup current partition for next iteration
dfs(0) #call dfs with start index = 0
return res #return result
def isPali(s, l, r): #parameters : string, left and right boundary
while l < r:
if s[l] != s[r]:
return False
l, r = l + 1, r - 1
return True
print(partition("aab"))
print(partition("a"))