From 8ae7c99272ca11a0bae8ed772823e12edc7a24f7 Mon Sep 17 00:00:00 2001 From: agnivg Date: Sat, 15 Oct 2022 15:37:17 +0530 Subject: [PATCH 1/7] Added code for palindrome partitioning problem under dynamic programming --- .../palindrome_partitioning.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 dynamic_programming/palindrome_partitioning.py diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py new file mode 100644 index 000000000000..95e66ffc36e3 --- /dev/null +++ b/dynamic_programming/palindrome_partitioning.py @@ -0,0 +1,39 @@ +""" +Given a string s, partition s such that every substring of the +partition is a palindrome. +Find the minimum cuts needed for a palindrome partitioning of s. + +Time Complexity: O(n^2) +Space Complexity: O(n^2) +For other explanations refer to: https://www.youtube.com/watch?v=_H8V5hJUGd0 +""" + + +def find_minimum_partitions(s): + """ + Returns the minimum cuts needed for a palindrome partitioning of s + + >>> find_minimum_partitions("aab") + 1 + >>> find_minimum_partitions("aaa") + 0 + >>> find_minimum_partitions("ababbbabbababa") + 3 + """ + n = len(s) + cut = [0 for i in range(n)] + ispalindrome = [[False for i in range(n)] for j in range(n)] + for i in range(n): + mincut = i + for j in range(i + 1): + if s[i] == s[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): + ispalindrome[j][i] = True + mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) + cut[i] = mincut + return cut[n - 1] + + +if __name__ == "__main__": + s = input("Enter the string: ").strip() + ans = find_minimum_partitions(s) + print(f"Minimum number of partitions required for the string is {ans}") From 9690b3996f1ac1e3b133330ad08add16085e0204 Mon Sep 17 00:00:00 2001 From: Agniv Ghosh <73717822+agnivg@users.noreply.github.com> Date: Sun, 16 Oct 2022 01:40:22 +0530 Subject: [PATCH 2/7] Updated return type for function --- dynamic_programming/palindrome_partitioning.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index 95e66ffc36e3..a90befc93aaf 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -9,9 +9,9 @@ """ -def find_minimum_partitions(s): +def find_minimum_partitions(string: str) -> int: """ - Returns the minimum cuts needed for a palindrome partitioning of s + Returns the minimum cuts needed for a palindrome partitioning of string >>> find_minimum_partitions("aab") 1 @@ -20,13 +20,13 @@ def find_minimum_partitions(s): >>> find_minimum_partitions("ababbbabbababa") 3 """ - n = len(s) + n = len(string) cut = [0 for i in range(n)] ispalindrome = [[False for i in range(n)] for j in range(n)] for i in range(n): mincut = i for j in range(i + 1): - if s[i] == s[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): + if string[i] == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): ispalindrome[j][i] = True mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) cut[i] = mincut From 5d5be656570082ba3bc118af35bc771b1153b366 Mon Sep 17 00:00:00 2001 From: Agniv Ghosh <73717822+agnivg@users.noreply.github.com> Date: Mon, 17 Oct 2022 02:41:38 +0530 Subject: [PATCH 3/7] Updated Line 24 according to suggestions --- dynamic_programming/palindrome_partitioning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index a90befc93aaf..5debc853b4b8 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -21,7 +21,7 @@ def find_minimum_partitions(string: str) -> int: 3 """ n = len(string) - cut = [0 for i in range(n)] + cut = [0] * n ispalindrome = [[False for i in range(n)] for j in range(n)] for i in range(n): mincut = i From 370c279d75fe08e2b3204c0f10735e624bb650aa Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 13:59:43 +0100 Subject: [PATCH 4/7] Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris --- dynamic_programming/palindrome_partitioning.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index 5debc853b4b8..3c59c57f311a 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -22,12 +22,12 @@ def find_minimum_partitions(string: str) -> int: """ n = len(string) cut = [0] * n - ispalindrome = [[False for i in range(n)] for j in range(n)] + is_palindromic = [[False for i in range(n)] for j in range(n)] for i in range(n): mincut = i for j in range(i + 1): if string[i] == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): - ispalindrome[j][i] = True + is_palindromic[j][i] = True mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) cut[i] = mincut return cut[n - 1] From 34ead4d76dc8779ae48cf49613572b112eed52ce Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 14:02:53 +0100 Subject: [PATCH 5/7] Update palindrome_partitioning.py --- dynamic_programming/palindrome_partitioning.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index 3c59c57f311a..7917da4fce86 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -20,13 +20,13 @@ def find_minimum_partitions(string: str) -> int: >>> find_minimum_partitions("ababbbabbababa") 3 """ - n = len(string) - cut = [0] * n - is_palindromic = [[False for i in range(n)] for j in range(n)] - for i in range(n): + length = len(string) + cut = [0] * length + is_palindromic = [[False for i in range(length)] for j in range(length)] + for i, c in enumerate(length): mincut = i for j in range(i + 1): - if string[i] == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): + if c == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): is_palindromic[j][i] = True mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) cut[i] = mincut @@ -36,4 +36,4 @@ def find_minimum_partitions(string: str) -> int: if __name__ == "__main__": s = input("Enter the string: ").strip() ans = find_minimum_partitions(s) - print(f"Minimum number of partitions required for the string is {ans}") + print(f"Minimum number of partitions required for the '{s}' is {ans}") From c886e31dd135c264c46357c9dc35af005d9cbe8f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 14:04:38 +0100 Subject: [PATCH 6/7] Update palindrome_partitioning.py --- dynamic_programming/palindrome_partitioning.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index 7917da4fce86..4c8c5c7a353e 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -26,11 +26,11 @@ def find_minimum_partitions(string: str) -> int: for i, c in enumerate(length): mincut = i for j in range(i + 1): - if c == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): + if c == string[j] and (i - j < 2 or is_palindrome[j + 1][i - 1]): is_palindromic[j][i] = True mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) cut[i] = mincut - return cut[n - 1] + return cut[length - 1] if __name__ == "__main__": From 048d0232a769e6446fa36dedaa72dabcaf4e3191 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 14:11:54 +0100 Subject: [PATCH 7/7] is_palindromic --- dynamic_programming/palindrome_partitioning.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/palindrome_partitioning.py b/dynamic_programming/palindrome_partitioning.py index 4c8c5c7a353e..c1629440ef2e 100644 --- a/dynamic_programming/palindrome_partitioning.py +++ b/dynamic_programming/palindrome_partitioning.py @@ -23,10 +23,10 @@ def find_minimum_partitions(string: str) -> int: length = len(string) cut = [0] * length is_palindromic = [[False for i in range(length)] for j in range(length)] - for i, c in enumerate(length): + for i, c in enumerate(string): mincut = i for j in range(i + 1): - if c == string[j] and (i - j < 2 or is_palindrome[j + 1][i - 1]): + if c == string[j] and (i - j < 2 or is_palindromic[j + 1][i - 1]): is_palindromic[j][i] = True mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) cut[i] = mincut