Skip to main content

Longest Palindrome After Substring Concatenation I - Solution & Explanation

MediumTwo PointersStringDynamic ProgrammingEnumeration11 min readAsked at: Meta, Google
Practice this problem

Problem Statement

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 <= 30
  • s and t consist of lowercase English letters.

Approach Overview

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.

Solution

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Substring EnumerationO(n^4)O(1)Understanding the problem or validating very small inputs
Center Expansion + Palindrome CheckO(n^3)O(1)Intermediate optimization without preprocessing
Enumerate Centers + Dynamic ProgrammingO(n^2)O(n^2)General optimal solution for interviews and large inputs

Video Solution

3503. Longest Palindrome After Substring Concatenation I | Weekly Contest 443 | Leetcode • Rapid Syntax • 1,660 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Longest Palindrome After Substring Concatenation I easy or hard?
The problem is generally classified as Medium difficulty. The brute force idea is straightforward, but reaching the optimal O(n^2) solution requires recognizing palindrome center expansion and combining it with dynamic programming for substring validation.
Longest Palindrome After Substring Concatenation I Python/Java solution
The typical implementation first builds a DP table for palindrome substrings and then iterates through possible centers using two pointers. The same logic translates directly across Python, Java, C++, Go, and TypeScript since it relies on simple array indexing and nested loops.
How to solve Longest Palindrome After Substring Concatenation I in O(n^2)?
Precompute a DP table where dp[i][j] indicates whether substring i..j is a palindrome. Then enumerate possible palindrome centers and expand outward using two pointers, comparing characters from the two selected substrings. When expansion stops, use the DP table to verify whether the remaining internal section forms a palindrome.
What is the best approach for Longest Palindrome After Substring Concatenation I?
The most efficient method enumerates palindrome centers and uses dynamic programming to precompute palindromic substrings. Center expansion checks how characters from both chosen substrings mirror each other, while the DP table instantly validates internal palindrome segments. This reduces repeated checks and achieves O(n^2) time complexity.
Is Longest Palindrome After Substring Concatenation I asked at Google/Amazon/Meta?
Problems involving palindrome expansion, substring concatenation, and dynamic programming frequently appear in interviews at companies like Google, Amazon, and Meta. Variants of longest palindrome and substring combination problems are common in technical screens.
What data structure is used in Longest Palindrome After Substring Concatenation I?
The core structure is a 2D dynamic programming table that records whether substrings are palindromes. The algorithm also relies on two-pointer expansion from palindrome centers to compare characters efficiently across concatenated substrings.
What is the time complexity of Longest Palindrome After Substring Concatenation I?
The optimal solution runs in O(n^2) time with O(n^2) space using center expansion combined with dynamic programming. Brute force enumeration of all substring pairs can reach O(n^4) time, which is too slow for typical constraints.

Ready to solve this problem?

Practice Longest Palindrome After Substring Concatenation I with our built-in code editor and test cases.

Practice on FleetCode