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 <= 30s and t consist of lowercase English letters.Problem Overview: You are given two strings and can choose any substring from each, concatenate them, and form a new string. The goal is to compute the maximum possible length of a palindrome that can be formed after this concatenation.
The challenge is that the palindrome may span across the boundary where the two substrings meet. A naive search over all substring pairs quickly becomes expensive, so the key is detecting palindromes efficiently while considering combinations from both strings.
Approach 1: Brute Force Substring Enumeration (O(n4) time, O(1) space)
Generate every substring of the first string and every substring of the second string. Concatenate each pair and check whether the result is a palindrome using a two‑pointer comparison from both ends. This requires iterating through O(n2) substrings per string, leading to O(n4) combinations overall. Palindrome validation itself costs O(n), making the total complexity even worse in practice. This approach is useful only for understanding the problem constraints or validating small inputs.
Approach 2: Enumerate Palindrome Centers + Dynamic Programming (O(n2) time, O(n2) space)
The optimized solution observes that any palindrome can be described by a center (single character or gap between characters). Instead of building every concatenated string, enumerate possible palindrome centers and expand outward using the classic center‑expansion technique from two pointers. While expanding, track how characters from the first and second substrings mirror each other.
Dynamic programming helps precompute whether substrings inside each string are palindromes. A DP table dp[i][j] stores whether the substring from index i to j forms a palindrome. With this information, once the cross‑boundary characters match, you can instantly verify whether the remaining portion inside either string is already a palindrome. This avoids repeatedly recomputing palindrome checks.
The algorithm effectively combines enumeration of palindrome centers with substring palindrome checks from dynamic programming. Each center expansion takes linear time and there are O(n2) relevant combinations across the two strings, producing an overall O(n2) runtime.
Recommended for interviews: Interviewers expect the center‑expansion + DP idea. Starting with brute force shows you understand the search space, but recognizing that palindromes can be expanded from centers and accelerated with DP demonstrates strong string algorithm intuition and optimization skills.
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^4) | O(1) | Understanding the problem or validating very small inputs |
| Center Expansion + Palindrome Check | O(n^3) | O(1) | Intermediate optimization without preprocessing |
| Enumerate Centers + Dynamic Programming | O(n^2) | O(n^2) | General optimal solution for interviews and large inputs |
3503. Longest Palindrome After Substring Concatenation I | Weekly Contest 443 | Leetcode • Rapid Syntax • 1,616 views views
Watch 4 more video solutions →Practice Longest Palindrome After Substring Concatenation I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor