From bb507dd7f4df500473274e137b1e3b36a8d42254 Mon Sep 17 00:00:00 2001 From: Exzrgs Date: Tue, 1 Oct 2024 14:26:59 +0900 Subject: [PATCH] complete --- NeetCode/palindrome-partitioning/step1.py | 92 +++++++++++++++++++++++ NeetCode/palindrome-partitioning/step3.py | 65 ++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 NeetCode/palindrome-partitioning/step1.py create mode 100644 NeetCode/palindrome-partitioning/step3.py diff --git a/NeetCode/palindrome-partitioning/step1.py b/NeetCode/palindrome-partitioning/step1.py new file mode 100644 index 0000000..bc742d7 --- /dev/null +++ b/NeetCode/palindrome-partitioning/step1.py @@ -0,0 +1,92 @@ +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + partitions = [] + + def check_palindrome(s): + if not s: + return False + + front = 0 + back = len(s) - 1 + while front <= back and s[front] == s[back]: + front += 1 + back -= 1 + return front > back + + def append_palindrome(index, partition): + if index == len(s): + if not check_palindrome(partition): + return + res.append(partitions.copy() + [partition]) + return + + append_palindrome(index + 1, partition + s[index]) + + if check_palindrome(partition): + partitions.append(partition) + append_palindrome(index + 1, s[index]) + partitions.pop() + + append_palindrome(0, "") + return res + +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + partitions = [] + + def check_palindrome(s): + if not s: + return False + + front = 0 + back = len(s) - 1 + while front <= back and s[front] == s[back]: + front += 1 + back -= 1 + return front > back + + def append_palindrome(index, partition): + if index == len(s): + if not check_palindrome(partition): + return + res.append(partitions.copy() + [partition]) + return + + append_palindrome(index + 1, partition + s[index]) + + if check_palindrome(partition): + partitions.append(partition) + append_palindrome(index + 1, s[index]) + partitions.pop() + + append_palindrome(0, "") + return res + +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + partitions = [] + + def check_palindrome(s, left, right): + while left < right: + if s[left] != s[right]: + return False + left += 1 + right -= 1 + return True + + def append_palindromes(index): + if index == len(s): + res.append(partitions.copy()) + return + + for j in range(index, len(s)): + if check_palindrome(s, index, j): + partitions.append(s[index : j + 1]) + append_palindromes(j + 1) + partitions.pop() + + append_palindromes(0) + return res diff --git a/NeetCode/palindrome-partitioning/step3.py b/NeetCode/palindrome-partitioning/step3.py new file mode 100644 index 0000000..756d10e --- /dev/null +++ b/NeetCode/palindrome-partitioning/step3.py @@ -0,0 +1,65 @@ +# time: O(2^n * n) generating all possible substrings, check if it is palindrome +# space: O(n) + +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + palindromes = [] + + def check_palindrome(s, left, right): + while left < right: + if s[left] != s[right]: + return False + left += 1 + right -= 1 + return True + + def append_palindrome(index): + if index == len(s): + res.append(palindromes.copy()) + return + + for j in range(index, len(s)): + if check_palindrome(s, index, j): + palindromes.append(s[index : j + 1]) + append_palindrome(j + 1) + palindromes.pop() + + append_palindrome(0) + return res + +class Solution: + def partition(self, s: str) -> List[List[str]]: + res = [] + palindromes = [] + + def check_palindrome(s): + if not s: + return False + + left = 0 + right = len(s) - 1 + while left < right: + if s[left] != s[right]: + return False + left += 1 + right -= 1 + return True + + def append_partitions(index, partition): + if index == len(s): + if not check_palindrome(partition): + return + res.append(palindromes.copy() + [partition]) + return + + partition += s[index] + append_partitions(index + 1, partition) + + if check_palindrome(partition): + palindromes.append(partition) + append_partitions(index + 1, "") + palindromes.pop() + + append_partitions(0, "") + return res