- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximize palindrome length from subsequences in Python

Suppose we have two strings, s and t. We want to make a string in the following manner −

Select some non-empty subsequence sub1 from s.

Select some non-empty subsequence sub2 from t.

Concatenate sub1 and sub2, to make the string.

We have to find the length of the longest palindrome that can be formed in the described manner. If we cannot make any palindrome, then return 0.

So, if the input is like s = "hillrace" t = "cargame", then the output will be 7 because we can take "race" from s and "car" from r, so "racecar" is the palindrome with length 7.

To solve this, we will follow these steps −

n := size of s, m := size of t

word := s + t

dp := make a 2D array of size (n+m) x (n+m) and fill with 0

for i in range (n + m - 1) to 0, decrease by 1, do

for j in range i to n + m - 1, do

if i is same as j, then

dp[i, j] := 1

otherwise when word[i] is same as word[j], then

dp[i, j] := 2 + dp[i + 1, j - 1]

otherwise,

dp[i, j] = maximum of dp[i + 1, j] and dp[i, j - 1]

ans := 0

for i in range 0 to n - 1, do

for j in range m - 1 to -1, decrease by 1, do

if s[i] is same as t[j], then

ans = maximum of ans and dp[i, n + j]

return ans

## Example

Let us see the following implementation to get better understanding

def solve(s, t): n, m = len(s), len(t) word = s + t dp = [[0] * (n + m) for _ in range(n + m)] for i in range(n + m - 1, -1,-1): for j in range(i, n + m): if i == j: dp[i][j] = 1 elif word[i] == word[j]: dp[i][j] = 2 + dp[i + 1][j - 1] else: dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) ans = 0 for i in range(n): for j in range(m - 1, -1, -1): if s[i] == t[j]: ans = max(ans, dp[i][n + j]) return ans s = "hillrace" t = "cargame" print(solve(s, t))

## Input

[[2,2],[1,2],[3,2]], [[3,1],[3,3],[5,2]]

## Output

7

- Related Questions & Answers
- Program to find length of longest chunked palindrome decomposition in Python
- Program to find number of distinct subsequences in Python
- Program to find n length string made of letters from m sized alphabet with no palindrome in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of different subsequences GCDs in Python
- Program to find maximize score after n operations in Python
- Program to find number of increasing subsequences of size k in Python
- Program to find length of longest matrix path length in Python
- Find the Nth Even Length Palindrome using C++
- Program to find length of longest valid parenthesis from given string in Python
- C++ program to find string with palindrome substring whose length is at most k
- Program to find minimum length of lossy Run-Length Encoding in Python
- Program to find length of shortest supersequence in Python
- Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
- Program to find length of longest Fibonacci subsequence from a given list in Python