-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add greedy method for longest palindromic subsequence #3958
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
The longest palindromic subsequence (LPS) is the longest | ||
subsequence in a string that is the same when reversed. | ||
A subsequence is not the same as a substring. | ||
Unlike substrings, the characters of subsequences are not required | ||
to occupy consecutive positions. | ||
|
||
Explanation: | ||
https://www.techiedelight.com/longest-palindromic-subsequence-using-dynamic-programming/ | ||
""" | ||
|
||
|
||
def longest_palindromic_subsequence( | ||
sequence: str, begin_substring: int, end_substring: int | ||
) -> int: | ||
"""Find the longest palindromic subsequence in a given string | ||
|
||
:param sequence: The sequence to search in | ||
:type sequence: str | ||
:param i: The initial lower index bounding the search area | ||
:type i: int | ||
:param j: The initial upper index bounding the search area | ||
:type j: int | ||
:return: The length of the longest palindromic subsequence | ||
:rtype: int | ||
|
||
>>> longest_palindromic_subsequence("ABBDCACB", 0, 7) | ||
5 | ||
>>> longest_palindromic_subsequence("nurses run", 0, 9) | ||
9 | ||
>>> longest_palindromic_subsequence("Z", 0, 0) | ||
1 | ||
""" | ||
# If the sequence only has one character, it is a palindrome | ||
# This prevents a one-letter palindrome from being counted as 2 | ||
if begin_substring == end_substring: | ||
return 1 | ||
|
||
# Base case: If i is greater than j you have gone too far | ||
if begin_substring > end_substring: | ||
return 0 | ||
|
||
# If the first character of the current string equals the last | ||
if sequence[begin_substring] == sequence[end_substring]: | ||
# Recur with the remaining substring | ||
return ( | ||
longest_palindromic_subsequence( | ||
sequence, begin_substring + 1, end_substring - 1 | ||
) | ||
+ 2 | ||
) | ||
|
||
# If the first and last characters are NOT equal, then find the larger LPS between: | ||
# a.) the substring formed by removing the first character | ||
# b.) the substring formed by removing the last character | ||
return max( | ||
longest_palindromic_subsequence(sequence, begin_substring + 1, end_substring), | ||
longest_palindromic_subsequence(sequence, begin_substring, end_substring - 1), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
sequence = input("Enter sequence string: ") | ||
len_lps = longest_palindromic_subsequence(sequence, 0, len(sequence) - 1) | ||
print(f'The length of the LPS in "{sequence}" is {len_lps}') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is a direct copy from the mentioned link.
@cclauss Please take a look at this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, only a few names are changed which was recommended by bot