Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11eebb5

Browse files
committedOct 14, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent f117362 commit 11eebb5

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed
 
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
def is_palindrome(s: str) -> bool:
22
"""
33
Helper function to check if a given string is a palindrome.
4-
4+
55
Args:
66
s (str): The string to check.
7-
7+
88
Returns:
99
bool: True if s is a palindrome, False otherwise.
1010
"""
1111
return s == s[::-1]
1212

13+
1314
def backtrack(start: int, path: list, result: list, s: str):
1415
"""
1516
Backtracking function to find all palindrome partitions of the string s.
16-
17+
1718
Args:
1819
start (int): Starting index of the substring to consider.
1920
path (list): The current path (partition) being constructed.
@@ -24,7 +25,7 @@ def backtrack(start: int, path: list, result: list, s: str):
2425
if start == len(s):
2526
result.append(path[:]) # Add a copy of the current path to the result
2627
return
27-
28+
2829
# Try every possible partition starting from 'start'
2930
for end in range(start + 1, len(s) + 1):
3031
# If the current substring is a palindrome, we can proceed
@@ -33,20 +34,22 @@ def backtrack(start: int, path: list, result: list, s: str):
3334
backtrack(end, path, result, s) # Explore further partitions
3435
path.pop() # Backtrack and remove the last chosen partition
3536

37+
3638
def partition(s: str) -> list:
3739
"""
3840
Main function to find all palindrome partitions of a string.
39-
41+
4042
Args:
4143
s (str): The input string.
42-
44+
4345
Returns:
4446
list: A list of lists containing all valid palindrome partitions.
4547
"""
4648
result = [] # List to store all partitions
4749
backtrack(0, [], result, s) # Start the backtracking process
4850
return result
4951

52+
5053
# Example usage:
5154
s = "aab"
5255
print(partition(s)) # Output: [['a', 'a', 'b'], ['aa', 'b']]

0 commit comments

Comments
 (0)
Please sign in to comment.