-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_partitioning.py
34 lines (30 loc) · 1.07 KB
/
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
from typing import List
class PalindromePartitioning:
def partition(self, s: str) -> List[List[str]]:
# List to store the final partitions
partitions = []
# Special case
if s is None or len(s) == 0:
return partitions
# Perform backtracking
self.backtrack(s, 0, [], partitions)
return partitions
def backtrack(self, s: str, index: int, current: List[str], partitions: List[List[str]]):
# Base case
if index == len(s):
partitions.append(list(current))
return
for i in range(index, len(s)):
# Check if the current substring is palindrome
if self.is_palindrome(s, index, i):
current.append(s[index:i + 1])
self.backtrack(s, i + 1, current, partitions)
current.pop()
@staticmethod
def is_palindrome(s: str, left: int, right: int):
while left <= right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True