blob: 2c28ad87911ab035c66d4945cb5268cd7506e656 (
plain)
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import List
def is_palindrome(s: str) -> bool:
return s == s[::-1]
def palindrome_partitions(s: str, start: int, current_partition: List[str],
result: List[List[str]]) -> None:
if start == len(s):
result.append(current_partition[:])
return
for end in range(start + 1, len(s) + 1):
substr = s[start:end]
if is_palindrome(substr):
current_partition.append(substr)
palindrome_partitions(s, end, current_partition, result)
current_partition.pop()
S = 'aabaab'
result = []
palindrome_partitions(S, 0, [], result)
if result:
print(f"There are {len(result)} possible solutions.")
for index, partition in enumerate(result, start=1):
print(f"{index}) {', '.join(partition)}")
else:
print("-1")
|