Longest Palindrome After Substring Concatenation II - Problem
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.
Note: A palindrome is a string that reads the same forward and backward. An empty string is considered a palindrome of length 0.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abc", t = "cba"
›
Output:
6
💡 Note:
Take full string from s ("abc") and full string from t ("cba") to form "abccba", which is a palindrome of length 6
Example 2 — Partial Substrings
$
Input:
s = "ab", t = "ba"
›
Output:
4
💡 Note:
Take "ab" from s and "ba" from t to form "abba", which is a palindrome of length 4
Example 3 — Empty Substring
$
Input:
s = "abc", t = "def"
›
Output:
1
💡 Note:
No good concatenation forms a long palindrome. Best is taking single character like "a" (from s) + "" (empty from t) = "a" with length 1
Constraints
- 1 ≤ s.length, t.length ≤ 1000
- s and t consist of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Strings
Two strings s and t from which we select substrings
2
Concatenate
Combine substring from s with substring from t
3
Find Maximum
Return length of longest palindrome possible
Key Takeaway
🎯 Key Insight: We need to systematically try different substring combinations to find the longest possible palindrome
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code