Skip to content

Added longest_palindromic_subsequence algorithm in Dynamic Programming with zero conflicts #9448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions dynamic_programming/longest_palindromic_subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
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]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: s

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input_string

"""
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()