Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
sand second part comes fromt
Algorithm Steps
Create a DP table where
dp[i][j]represents the longest palindromic subsequence in substring from indexitojFill the DP table using bottom-up approach
For each character in
sand each character intthat 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 ?
DP Table Construction: We build a table where
dp[i][j]stores the length of the longest palindromic subsequence in the substring from indexitojof the combined stringPalindrome Search: We iterate through all pairs of matching characters where one comes from string
sand another from stringt, 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.
