From 248321c98b41c5ea04f9f5306fd2cd7f15ba31ca Mon Sep 17 00:00:00 2001 From: N16H7H4WK-3R Date: Mon, 2 Oct 2023 15:30:18 +0530 Subject: [PATCH 1/4] added longest_palindromic_subsequence algorithm --- .../longest_palindromic_subsequence.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 dynamic_programming/longest_palindromic_subsequence.py diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py new file mode 100644 index 000000000000..ef389d866c42 --- /dev/null +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -0,0 +1,70 @@ +""" +LPS Problem Statement: Find the length of the longest palindromic subsequence +in a given string. A palindromic subsequence is a sequence that reads the same +forward and backward, but not necessarily continuous. + +Example: For the input "babad," the longest palindromic subsequence can be "bab" or "aba." +""" + + +def longest_palindromic_subsequence(s: str) -> tuple[int, str]: + """ + Finds the length and the longest palindromic subsequence in a given string. + + Parameters: + s (str): The input string. + + Returns: + Tuple[int, str]: A tuple containing the length of the longest palindromic subsequence + and the subsequence itself. + + Raises: + ValueError: If the input string is empty or None. + + Example: + >>> longest_palindromic_subsequence("bbbab") + (4, 'bbbb') + >>> longest_palindromic_subsequence("babad") + (3, 'bab') + >>> longest_palindromic_subsequence("cbbd") + (2, 'bb') + """ + if s is None or len(s) == 0: + raise ValueError("Input string cannot be empty or None") + + n = len(s) + #create a table to store results of subproblems + dp = [[0] * n for _ in range(n)] + + for i in range(n): + dp[i][i] = 1 + + for cl in range(2, n + 1): + for i in range(n - cl + 1): + j = i + cl - 1 + if s[i] == s[j] and cl == 2: + dp[i][j] = 2 + elif s[i] == s[j]: + dp[i][j] = dp[i + 1][j - 1] + 2 + else: + dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]) + + # Reconstruct the Longest Palindromic Subsequence + i, j = 0, n - 1 + lps = [] + while i < n and j >= 0: + if s[i] == s[j]: + lps.append(s[i]) + i += 1 + j -= 1 + elif i < j and dp[i][j - 1] >= dp[i + 1][j]: + j -= 1 + else: + i += 1 + + return dp[0][n - 1], "".join(lps) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 2d4f5044aefc9f85c22514fecd462e4cf45e65c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:05:29 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/longest_palindromic_subsequence.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py index ef389d866c42..8be9a990f9a0 100644 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -33,7 +33,7 @@ def longest_palindromic_subsequence(s: str) -> tuple[int, str]: raise ValueError("Input string cannot be empty or None") n = len(s) - #create a table to store results of subproblems + # create a table to store results of subproblems dp = [[0] * n for _ in range(n)] for i in range(n): @@ -67,4 +67,5 @@ def longest_palindromic_subsequence(s: str) -> tuple[int, str]: if __name__ == "__main__": import doctest + doctest.testmod() From daf03c39b4671af4e0096ad8f97b995c5598cd80 Mon Sep 17 00:00:00 2001 From: N16H7H4WK-3R Date: Mon, 2 Oct 2023 16:44:40 +0530 Subject: [PATCH 3/4] added longest_palindromic_subsequence algorithm --- dynamic_programming/longest_palindromic_subsequence.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py index 8be9a990f9a0..89f623fe37cf 100644 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -3,7 +3,8 @@ in a given string. A palindromic subsequence is a sequence that reads the same forward and backward, but not necessarily continuous. -Example: For the input "babad," the longest palindromic subsequence can be "bab" or "aba." +Example: For the input "babad," the longest palindromic subsequence +can be "bab" or "aba." """ @@ -15,8 +16,8 @@ def longest_palindromic_subsequence(s: str) -> tuple[int, str]: s (str): The input string. Returns: - Tuple[int, str]: A tuple containing the length of the longest palindromic subsequence - and the subsequence itself. + Tuple[int, str]: A tuple containing the length of the longest + palindromic subsequence and the subsequence itself. Raises: ValueError: If the input string is empty or None. @@ -33,7 +34,7 @@ def longest_palindromic_subsequence(s: str) -> tuple[int, str]: raise ValueError("Input string cannot be empty or None") n = len(s) - # create a table to store results of subproblems + #create a table to store results of subproblems dp = [[0] * n for _ in range(n)] for i in range(n): From 949b5df360a7632ba2096e3ca9ac916023a01918 Mon Sep 17 00:00:00 2001 From: N16H7H4WK-3R Date: Mon, 2 Oct 2023 16:50:28 +0530 Subject: [PATCH 4/4] Resolved Conflicts --- dynamic_programming/longest_palindromic_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py index 89f623fe37cf..77c05ac8447a 100644 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -34,7 +34,7 @@ def longest_palindromic_subsequence(s: str) -> tuple[int, str]: raise ValueError("Input string cannot be empty or None") n = len(s) - #create a table to store results of subproblems + # Create a table to store results of subproblems dp = [[0] * n for _ in range(n)] for i in range(n):