Longest Palindrome After Substring Concatenation I - 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.
A palindrome is a string that reads the same forwards and backwards.
Input & Output
Example 1 — Basic Case
$
Input:
s = "ab", t = "ba"
›
Output:
3
💡 Note:
Select substring "a" from s and "ba" from t to form "aba", which is a palindrome of length 3. This is the maximum possible.
Example 2 — Single Character
$
Input:
s = "x", t = "y"
›
Output:
1
💡 Note:
Best we can do is select "x" from s (and empty from t) or "y" from t (and empty from s), giving palindromes of length 1.
Example 3 — Identical Strings
$
Input:
s = "abc", t = "abc"
›
Output:
3
💡 Note:
We can select "ab" from s and "a" from t to form "aba", which is a palindrome of length 3. This is the maximum possible length.
Constraints
- 0 ≤ s.length, t.length ≤ 1000
- s and t consist of lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code