From f1173629e092de7605697ef8af408602187e1bfc Mon Sep 17 00:00:00 2001 From: Mahfooz Alam <98471514+Mahfoozalam1516@users.noreply.github.com> Date: Mon, 14 Oct 2024 16:29:02 +0530 Subject: [PATCH 1/2] Create Palindrome_partitioning.py --- backtracking/Palindrome_partitioning.py | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 backtracking/Palindrome_partitioning.py diff --git a/backtracking/Palindrome_partitioning.py b/backtracking/Palindrome_partitioning.py new file mode 100644 index 000000000000..49aac74128a3 --- /dev/null +++ b/backtracking/Palindrome_partitioning.py @@ -0,0 +1,52 @@ +def is_palindrome(s: str) -> bool: + """ + Helper function to check if a given string is a palindrome. + + Args: + s (str): The string to check. + + Returns: + bool: True if s is a palindrome, False otherwise. + """ + return s == s[::-1] + +def backtrack(start: int, path: list, result: list, s: str): + """ + Backtracking function to find all palindrome partitions of the string s. + + Args: + start (int): Starting index of the substring to consider. + path (list): The current path (partition) being constructed. + result (list): The final list of all valid palindrome partitions. + s (str): The input string. + """ + # If we've reached the end of the string, add the current path to the result + if start == len(s): + result.append(path[:]) # Add a copy of the current path to the result + return + + # Try every possible partition starting from 'start' + for end in range(start + 1, len(s) + 1): + # If the current substring is a palindrome, we can proceed + if is_palindrome(s[start:end]): + path.append(s[start:end]) # Choose the current palindrome substring + backtrack(end, path, result, s) # Explore further partitions + path.pop() # Backtrack and remove the last chosen partition + +def partition(s: str) -> list: + """ + Main function to find all palindrome partitions of a string. + + Args: + s (str): The input string. + + Returns: + list: A list of lists containing all valid palindrome partitions. + """ + result = [] # List to store all partitions + backtrack(0, [], result, s) # Start the backtracking process + return result + +# Example usage: +s = "aab" +print(partition(s)) # Output: [['a', 'a', 'b'], ['aa', 'b']] From 11eebb56f165fbcd2cdb9631b45e2a7dc27baff9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:02:25 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/Palindrome_partitioning.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backtracking/Palindrome_partitioning.py b/backtracking/Palindrome_partitioning.py index 49aac74128a3..811daadefc96 100644 --- a/backtracking/Palindrome_partitioning.py +++ b/backtracking/Palindrome_partitioning.py @@ -1,19 +1,20 @@ def is_palindrome(s: str) -> bool: """ Helper function to check if a given string is a palindrome. - + Args: s (str): The string to check. - + Returns: bool: True if s is a palindrome, False otherwise. """ return s == s[::-1] + def backtrack(start: int, path: list, result: list, s: str): """ Backtracking function to find all palindrome partitions of the string s. - + Args: start (int): Starting index of the substring to consider. path (list): The current path (partition) being constructed. @@ -24,7 +25,7 @@ def backtrack(start: int, path: list, result: list, s: str): if start == len(s): result.append(path[:]) # Add a copy of the current path to the result return - + # Try every possible partition starting from 'start' for end in range(start + 1, len(s) + 1): # If the current substring is a palindrome, we can proceed @@ -33,13 +34,14 @@ def backtrack(start: int, path: list, result: list, s: str): backtrack(end, path, result, s) # Explore further partitions path.pop() # Backtrack and remove the last chosen partition + def partition(s: str) -> list: """ Main function to find all palindrome partitions of a string. - + Args: s (str): The input string. - + Returns: list: A list of lists containing all valid palindrome partitions. """ @@ -47,6 +49,7 @@ def partition(s: str) -> list: backtrack(0, [], result, s) # Start the backtracking process return result + # Example usage: s = "aab" print(partition(s)) # Output: [['a', 'a', 'b'], ['aa', 'b']]