From c2885c815024703435a4e93b3ba0ee447d131fc6 Mon Sep 17 00:00:00 2001 From: sanketkittad Date: Sun, 1 Oct 2023 17:50:37 +0530 Subject: [PATCH 1/5] added longest palindromic subsequence --- .../longest_palindromic_subsequence.py | 41 +++++++++++++++++++ 1 file changed, 41 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..a435fe159f23 --- /dev/null +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -0,0 +1,41 @@ +""" +author: Sanket Kittad +Given a string s, find the longest palindromic subsequence's length in s. +Input: s = "bbbab" +Output: 4 +Explanation: One possible longest palindromic subsequence is "bbbb". +""" + + +def longest_palindromic_subsequence(s: str) -> int: + """ + This function returns the longest palindromic subsequence in a string + >>> longest_palindromic_subsequence("bbbab") + 4 + >>> longest_palindromic_subsequence("bbabcbcab") + 7 + """ + n = len(s) + rev = s[::-1] + m = len(rev) + dp = [[-1] * (m + 1) for i in range(n + 1)] + for i in range(n + 1): + dp[i][0] = 0 + for i in range(m + 1): + dp[0][i] = 0 + for i in range(1, n + 1): + for j in range(1, m + 1): + # If characters at i and j are the same + # include them in the palindromic subsequence + if s[i - 1] == rev[j - 1]: + dp[i][j] = 1 + dp[i - 1][j - 1] + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + + return dp[n][m] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 50700da9949b0094e1460b29d26572518657fa9f Mon Sep 17 00:00:00 2001 From: sanketkittad Date: Sun, 1 Oct 2023 17:51:57 +0530 Subject: [PATCH 2/5] removed --- .../longest_palindromic_subsequence.py | 41 ------------------- 1 file changed, 41 deletions(-) delete mode 100644 dynamic_programming/longest_palindromic_subsequence.py diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py deleted file mode 100644 index a435fe159f23..000000000000 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -author: Sanket Kittad -Given a string s, find the longest palindromic subsequence's length in s. -Input: s = "bbbab" -Output: 4 -Explanation: One possible longest palindromic subsequence is "bbbb". -""" - - -def longest_palindromic_subsequence(s: str) -> int: - """ - This function returns the longest palindromic subsequence in a string - >>> longest_palindromic_subsequence("bbbab") - 4 - >>> longest_palindromic_subsequence("bbabcbcab") - 7 - """ - n = len(s) - rev = s[::-1] - m = len(rev) - dp = [[-1] * (m + 1) for i in range(n + 1)] - for i in range(n + 1): - dp[i][0] = 0 - for i in range(m + 1): - dp[0][i] = 0 - for i in range(1, n + 1): - for j in range(1, m + 1): - # If characters at i and j are the same - # include them in the palindromic subsequence - if s[i - 1] == rev[j - 1]: - dp[i][j] = 1 + dp[i - 1][j - 1] - else: - dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - - return dp[n][m] - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 7d934c5685f7782caa155c280c88ad10b0d8337a Mon Sep 17 00:00:00 2001 From: sanketkittad Date: Sun, 1 Oct 2023 17:52:39 +0530 Subject: [PATCH 3/5] added longest palindromic subsequence --- .../longest_palindromic_subsequence.py | 41 +++++++++++++++++++ 1 file changed, 41 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..a435fe159f23 --- /dev/null +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -0,0 +1,41 @@ +""" +author: Sanket Kittad +Given a string s, find the longest palindromic subsequence's length in s. +Input: s = "bbbab" +Output: 4 +Explanation: One possible longest palindromic subsequence is "bbbb". +""" + + +def longest_palindromic_subsequence(s: str) -> int: + """ + This function returns the longest palindromic subsequence in a string + >>> longest_palindromic_subsequence("bbbab") + 4 + >>> longest_palindromic_subsequence("bbabcbcab") + 7 + """ + n = len(s) + rev = s[::-1] + m = len(rev) + dp = [[-1] * (m + 1) for i in range(n + 1)] + for i in range(n + 1): + dp[i][0] = 0 + for i in range(m + 1): + dp[0][i] = 0 + for i in range(1, n + 1): + for j in range(1, m + 1): + # If characters at i and j are the same + # include them in the palindromic subsequence + if s[i - 1] == rev[j - 1]: + dp[i][j] = 1 + dp[i - 1][j - 1] + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + + return dp[n][m] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e5736a4800b5056d6eb6ed323c151bcf42ff8842 Mon Sep 17 00:00:00 2001 From: sanketkittad Date: Sun, 1 Oct 2023 17:55:24 +0530 Subject: [PATCH 4/5] added longest palindromic subsequence link --- dynamic_programming/longest_palindromic_subsequence.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py index a435fe159f23..6e428af44cb0 100644 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -4,6 +4,7 @@ Input: s = "bbbab" Output: 4 Explanation: One possible longest palindromic subsequence is "bbbb". +Leetcode link: https://leetcode.com/problems/longest-palindromic-subsequence/description/ """ From b3b0bcf624c2b1bd69d4a9a4f9be9f60d8a70032 Mon Sep 17 00:00:00 2001 From: sanketkittad Date: Sun, 1 Oct 2023 18:03:31 +0530 Subject: [PATCH 5/5] added comments --- dynamic_programming/longest_palindromic_subsequence.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/longest_palindromic_subsequence.py b/dynamic_programming/longest_palindromic_subsequence.py index 6e428af44cb0..a60d95e460e6 100644 --- a/dynamic_programming/longest_palindromic_subsequence.py +++ b/dynamic_programming/longest_palindromic_subsequence.py @@ -8,7 +8,7 @@ """ -def longest_palindromic_subsequence(s: str) -> int: +def longest_palindromic_subsequence(input_string: str) -> int: """ This function returns the longest palindromic subsequence in a string >>> longest_palindromic_subsequence("bbbab") @@ -16,19 +16,21 @@ def longest_palindromic_subsequence(s: str) -> int: >>> longest_palindromic_subsequence("bbabcbcab") 7 """ - n = len(s) - rev = s[::-1] + n = len(input_string) + rev = input_string[::-1] m = len(rev) dp = [[-1] * (m + 1) for i in range(n + 1)] for i in range(n + 1): dp[i][0] = 0 for i in range(m + 1): dp[0][i] = 0 + + # create and initialise dp array for i in range(1, n + 1): for j in range(1, m + 1): # If characters at i and j are the same # include them in the palindromic subsequence - if s[i - 1] == rev[j - 1]: + if input_string[i - 1] == rev[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])