Skip to content

Commit 876a32b

Browse files
committed
Palindrome Partitioning
1 parent 0ce869f commit 876a32b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''Leetcode- https://leetcode.com/problems/palindrome-partitioning/ '''
2+
'''
3+
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
4+
5+
A palindrome string is a string that reads the same backward as forward.
6+
7+
Example 1:
8+
9+
Input: s = "aab"
10+
Output: [["a","a","b"],["aa","b"]]
11+
'''
12+
def partition(s):
13+
res, part = [], []
14+
15+
def dfs(i):
16+
if i >= len(s):
17+
res.append(part.copy())
18+
return
19+
for j in range(i, len(s)):
20+
if isPali(s, i, j):
21+
part.append(s[i:j+1])
22+
dfs(j + 1)
23+
part.pop()
24+
dfs(0)
25+
return res
26+
27+
def isPali( s, l, r):
28+
while l < r:
29+
if s[l] != s[r]:
30+
return False
31+
l, r = l + 1, r - 1
32+
return True
33+
34+
#T :O(2^n)
35+
#S :O(n)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
1717
- [x] [Combination Sum II](Backtracking/40-Combination-Sum-II.py)
1818
- [x] [Combination Sum III](Backtracking/216-Combination-Sum-III.py)
1919
- [x] [Generate Parentheses](Backtracking/22-Generate-Parentheses.py)
20+
- [x] [Palindrome Partitioning](Backtracking/22-Generate-Parentheses.py)
2021

2122

0 commit comments

Comments
 (0)