Program to find maximize palindrome length from subsequences in Python

Given two strings s and t, we need to find the maximum length palindrome that can be formed by concatenating a subsequence from s with a subsequence from t. A subsequence preserves the relative order of characters but doesn't need to be contiguous.

Problem Approach

The key insight is to use dynamic programming to find the longest palindromic subsequence in the combined string, then check all possible ways to form palindromes using characters from both strings ?

  • Concatenate both strings to form a combined string

  • Use DP to find longest palindromic subsequence for any substring

  • Check all combinations where first part comes from s and second part comes from t

Algorithm Steps

  1. Create a DP table where dp[i][j] represents the longest palindromic subsequence in substring from index i to j

  2. Fill the DP table using bottom-up approach

  3. For each character in s and each character in t that match, calculate the maximum palindrome length

Example

Let's trace through the example with s = "hillrace" and t = "cargame" ?

def solve(s, t):
    n, m = len(s), len(t)
    word = s + t
    dp = [[0] * (n + m) for _ in range(n + m)]
    
    # Fill DP table for longest palindromic subsequence
    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])
    
    # Find maximum palindrome using subsequences from both strings
    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"
result = solve(s, t)
print(f"Maximum palindrome length: {result}")

# Let's trace the palindrome formation
print(f"String s: {s}")
print(f"String t: {t}")
print("We can take 'race' from s and 'car' from t to form 'racecar'")
Maximum palindrome length: 7
String s: hillrace
String t: cargame
We can take 'race' from s and 'car' from t to form 'racecar'

How It Works

The algorithm works in two phases ?

  1. DP Table Construction: We build a table where dp[i][j] stores the length of the longest palindromic subsequence in the substring from index i to j of the combined string

  2. Palindrome Search: We iterate through all pairs of matching characters where one comes from string s and another from string t, then check the maximum palindrome that can be formed

Time and Space Complexity

Aspect Complexity Explanation
Time O((n+m)²) DP table filling + nested loops
Space O((n+m)²) 2D DP table storage

Conclusion

This dynamic programming approach efficiently finds the maximum palindrome length by combining subsequences from two strings. The key insight is using the longest palindromic subsequence DP pattern on the concatenated string, then checking all valid character pairs from both strings.

Updated on: 2026-03-26T14:45:45+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements