Watch 5 video solutions for Longest Palindrome After Substring Concatenation I, a medium level problem involving Two Pointers, String, Dynamic Programming. This walkthrough by Rapid Syntax has 1,616 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |