Longest Palindromic Subsequence in Python Last Updated : 22 May, 2024 Comments Improve Suggest changes Like Article Like Report Longest Palindromic Subsequence (LPS) problem is about finding the longest subsequence of the given sequence which is a palindrome. In Python, the task of maximizing the number of words in a sentence can be solved by the methods of dynamic programming. The algorithm for finding the Longest Palindromic Subsequence involves creating a table to store the lengths of the longest palindromic subsequences for different substrings of the input sequence. Longest Palindromic Subsequence in Python using Memoization:Step-by-step algorithm: Define a recursive function, say lps(s1, s2, n1, n2) which finds the longest common subsequence among s1 and s2 where n1 is index of s1 and n2 is index of s2.Call the lps function with input string seq as s1 and reverse of seq as s2.If we reach the first index of any string, return 0Check if we have already computed the result for the current indices.If the result is already computed, return the result.Otherwise, if the current indices of both s1 and s2 are equal, dp[n1][n2] = 1 + lps(s1, s2, n1 - 1, n2 - 1)Else if the current index of both s1 and s2 are not equal, dp[n1][n2] = max(lps(s1, s2, n1 - 1, n2), lps(s1, s2, n1, n2 - 1))Store the above result in dp[n1][n2]Below is the implementation of Longest Palindromic Subsequence in Python using Memoization: Python # A Dynamic Programming based Python program for LPS problem # Returns the length of the longest palindromic subsequence # in seq dp = [[-1 for i in range(1001)]for j in range(1001)] # Returns the length of the longest palindromic subsequence # in seq def lps(s1, s2, n1, n2): if (n1 == 0 or n2 == 0): return 0 if (dp[n1][n2] != -1): return dp[n1][n2] if (s1[n1 - 1] == s2[n2 - 1]): dp[n1][n2] = 1 + lps(s1, s2, n1 - 1, n2 - 1) return dp[n1][n2] else: dp[n1][n2] = max(lps(s1, s2, n1 - 1, n2), lps(s1, s2, n1, n2 - 1)) return dp[n1][n2] # Driver program to test above functions seq = "GEEKSFORGEEKS" n = len(seq) s2 = seq s2 = s2[::-1] print(f"The length of the LPS is {lps(s2, seq, n, n)}") OutputThe length of the LPS is 5 Time Complexity: O(n2), where n is the length of the input stringAuxiliary Space: O(n2) Longest Palindromic Subsequence in Python using Tabulation:Step-by-step algorithm: Initialize a 2D array dp with dimensions (n x n), where n is the length of the input sequence.Set the diagonal elements of dp to 1, as each single character is a palindrome of length 1.Traverse the input sequence with two pointers i and j, where i starts from n-1 and decrements, and j starts from i+1 and increments.If seq[i] is equal to seq[j], set dp[i][j] to dp[i+1][j-1] + 2 because the current characters form a palindromic subsequence of length 2 greater than the one inside them.Otherwise, set dp[i][j] to the maximum of dp[i+1][j] and dp[i][j-1], as we discard one character from either end and try to find the longest palindromic subsequence.The result is stored in dp[0][n-1], which represents the length of the Longest Palindromic Subsequence.Below is the implementation of Longest Palindromic Subsequence in Python using Tabulation: Python def longestPalinSubseq(S): R = S[::-1] # dp[i][j] will store the length of the longest # palindromic subsequence for the substring # starting at index i and ending at index j dp = [[0] * (len(R) + 1) for _ in range(len(S) + 1)] # Filling up DP table based on conditions discussed # in the above approach for i in range(1, len(S) + 1): for j in range(1, len(R) + 1): if S[i - 1] == R[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) # At the end, DP table will contain the LPS # So just return the length of LPS return dp[len(S)][len(R)] # Driver code s = "GEEKSFORGEEKS" print("The length of the LPS is", longestPalinSubseq(s)) OutputThe length of the LPS is 5 Time Complexity: O(n2), where n is the length of the input stringAuxiliary Space: O(n2) Comment More infoAdvertise with us Next Article Longest Palindromic Subsequence in Python A amit7822i5n Follow Improve Article Tags : Strings Dynamic Programming DSA Python-DSA Practice Tags : Dynamic ProgrammingStrings Similar Reads Longest Palindromic Subsequence in C++ In this article, we will learn how to find the Longest Palindromic Subsequence (LPS) of a sequence using C++ programming language. LPS is the longest subsequence of a given sequence that reads the same backward as forward.Example:Input:Sequence: "BBABCBCAB"Output:Length of LPS is 7Explanation: The L 7 min read Print Longest Palindromic Subsequence Given a sequence, print a longest palindromic subsequence of it. Examples : Input : BBABCBCAB Output : BABCBAB The above output is the longest palindromic subsequence of given sequence. "BBBBB" and "BBCBB" are also palindromic subsequences of the given sequence, but not the longest ones. Input : GEE 11 min read Find Longest Palindromic Subsequence II Given a string s, return the length of the longest good palindromic subsequence in s such that:It has an even length.No two consecutive characters are equal, except the two middle ones.Example:Input: s = "bbabab"Output: 4Explanation: The longest good palindromic subsequence of s is "baab".Input: s = 8 min read Longest Palindromic Subsequence (LPS) Given a string s, find the length of the Longest Palindromic Subsequence in it. Note: The Longest Palindromic Subsequence (LPS) is the maximum-length subsequence of a given string that is also a Palindrome. Longest Palindromic SubsequenceExamples:Input: s = "bbabcbcab"Output: 7Explanation: Subsequen 15+ min read Printing Longest Bitonic Subsequence The Longest Bitonic Subsequence problem is to find the longest subsequence of a given sequence such that it is first increasing and then decreasing. A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bito 12 min read Longest palindrome subsequence with O(n) space Given a sequence, find the length of the longest palindromic subsequence in it. Examples: Input : abbaab Output : 4 Input : geeksforgeeks Output : 5 We have discussed a Dynamic Programming solution for Longest Palindromic Subsequence which is based on below recursive formula. // Every single charact 8 min read Printing Longest Common Subsequence Given two sequences, print the longest subsequence present in both of them. Examples: LCS for input Sequences âABCDGHâ and âAEDFHRâ is âADHâ of length 3. LCS for input Sequences âAGGTABâ and âGXTXAYBâ is âGTABâ of length 4.We have discussed Longest Common Subsequence (LCS) problem in a previous post 15+ min read Longest Repeating Subsequence Given a string s, the task is to find the length of the longest repeating subsequence, such that the two subsequences don't have the same string character at the same position, i.e. any ith character in the two subsequences shouldn't have the same index in the original string. Examples:Input: s= "ab 15+ min read Longest Palindromic Subsequence of two distinct characters Given a string S of lowercase letters, the task is to find the length of the longest palindromic subsequence made up of two distinct characters only. Examples: Input: S = "bbccdcbb" Output: 7 Explanation: The longest palindromic subsequence of the desired form is "bbcccbb", which is of length 7.Inpu 8 min read Longest alternative parity subsequence We are given an array a of size N. The task is to print the length of the longest alternative odd/even or even/odd subsequence. Examples: Input: a[] = { 13, 16, 8, 9, 32, 10 } Output: 4 {13, 16, 9, 10} or any other subsequence of length 4 can be the answer. Input: a[] = {1, 2, 3, 3, 9} Output: 3 App 7 min read Like