You are given two strings, s and t.
You can create a new string by selecting a substring from s (possibly empty) and a substring from t (possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
Example 1:
Input: s = "a", t = "a"
Output: 2
Explanation:
Concatenating "a" from s and "a" from t results in "aa", which is a palindrome of length 2.
Example 2:
Input: s = "abc", t = "def"
Output: 1
Explanation:
Since all characters are different, the longest palindrome is any single character, so the answer is 1.
Example 3:
Input: s = "b", t = "aaaa"
Output: 4
Explanation:
Selecting "aaaa" from t is the longest palindrome, so the answer is 4.
Example 4:
Input: s = "abcde", t = "ecdba"
Output: 5
Explanation:
Concatenating "abc" from s and "ba" from t results in "abcba", which is a palindrome of length 5.
Constraints:
1 <= s.length, t.length <= 1000s and t consist of lowercase English letters.Problem Overview: You are given two strings and can pick a substring from each, concatenate them, and form a new string. The task is to compute the maximum possible length of a palindrome that can be created from such a concatenation.
Approach 1: Brute Force Substring Enumeration (O(n^2 * m^2 * L) time, O(1) space)
Enumerate every substring from the first string and every substring from the second string. Concatenate them and check whether the resulting string is a palindrome using a two-pointer scan from both ends. If the string length is L, the palindrome check costs O(L). Since there are O(n^2) substrings in the first string and O(m^2) in the second, the total work grows quickly. This approach is mainly useful to understand the search space but becomes infeasible for larger inputs.
Approach 2: Enumerate Palindrome Centers + Dynamic Programming (O(n*m + n^2 + m^2) time, O(n^2 + m^2) space)
The key observation is that a valid palindrome can span the boundary between the chosen substrings. Treat that boundary as the center of the palindrome and expand outward using the two pointers technique. While expanding, characters must match between the suffix of the first substring and the prefix of the second substring.
Precompute palindrome information inside each string using dynamic programming. A DP table dp[i][j] indicates whether s[i..j] is a palindrome. This lets you extend a cross-string match with additional characters from either side if they already form a palindrome internally. During enumeration, match characters across both strings and track the longest symmetric expansion.
This converts the problem from checking all concatenations to expanding around possible centers and reusing precomputed palindrome segments. Most of the work becomes linear scans with constant-time DP lookups. The combination of center expansion and DP pruning drastically reduces repeated palindrome checks while still covering every valid construction.
Recommended for interviews: The palindrome-center enumeration with dynamic programming is the expected solution. Interviewers want to see recognition that concatenation palindromes behave like center expansions across two strings. Mentioning the brute force approach first shows understanding of the search space, while the optimized solution demonstrates skill with string processing, two-pointer expansion, and DP preprocessing.
According to the problem description, the concatenated palindrome string can be composed entirely of string s, entirely of string t, or a combination of both strings s and t. Additionally, there may be extra palindromic substrings in either string s or t.
Therefore, we first reverse string t and preprocess arrays g1 and g2, where g1[i] represents the length of the longest palindromic substring starting at index i in string s, and g2[i] represents the length of the longest palindromic substring starting at index i in string t.
We can initialize the answer ans as the maximum value in g1 and g2.
Next, we define f[i][j] as the length of the palindromic substring ending at the i-th character of string s and the j-th character of string t.
For f[i][j], if s[i - 1] equals t[j - 1], then f[i][j] = f[i - 1][j - 1] + 1. We then update the answer:
$
ans = max(ans, f[i][j] times 2 + (0 if i geq m else g1[i])) \
ans = max(ans, f[i][j] times 2 + (0 if j geq n else g2[j]))
Finally, we return the answer ans.
The time complexity is O(m times (m + n)), and the space complexity is O(m times n), where m and n are the lengths of strings s and t$, respectively.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Substring Enumeration | O(n^2 * m^2 * L) | O(1) | Useful for understanding the search space or validating small inputs |
| Enumerate Palindrome Centers + Dynamic Programming | O(n*m + n^2 + m^2) | O(n^2 + m^2) | General case and interview solution when strings are moderately large |
Longest Palindrome After Substring Concatenation II (Leetcode Weekly 443) • Soumya Bhattacharjee • 1,209 views views
Watch 3 more video solutions →Practice Longest Palindrome After Substring Concatenation II with our built-in code editor and test cases.
Practice on FleetCode