Problem Description
Given two strings s and t, you may pick any contiguous substring from s and any contiguous substring from t and concatenate them (s‐substring comes first, then t‐substring) to form a new string. Return the length of the longest palindrome that can be formed this way.
Key Insights
- The answer may come from a substring chosen solely from s or solely from t (since one of the substrings may be empty).
- When using both strings, the final palindrome must have a “mirrored” structure. In particular, if you let a be the substring chosen from s and b be the substring chosen from t such that a+b is a palindrome, then there exists some length L ≥ 0 with: • The first L characters of a equal the reverse of the last L characters of b. • The “middle” remainder (the part of a after the first L characters or the part of b before those L characters) must itself be a palindrome.
- One can “decompose” any cross-string palindrome into the form X + Y + reverse(X), where X is a contiguous string (the common “bridge”) and Y is a palindromic substring.
- Precomputing, for each index, the longest contiguous palindrome that starts at that index (for s) or ends at that index (for t) helps “glue” the two pieces together.
- Iterating over possible “mirrored” segments between s and t (using two pointers to expand matching portions between s and t in reverse order) leads to candidate palindromes of length 2*|X| plus an extra palindromic extension from one side.
Space and Time Complexity
Time Complexity: O(nmmin(n,m)) in the worst case – iterating over every pair (i in s, j in t) then expanding for the mirror portion. Space Complexity: O(n + m) for storing precomputed palindrome lengths for s and t.
Solution
We can break the problem into two parts. First, note that one may simply take a single substring from s or from t if it is a palindrome; so we precompute the longest palindrome (as a contiguous substring) that starts at each index in s and the longest that ends at each index in t. These precomputed arrays (lpS and lpT_end) allow us to “extend” a palindrome after a common prefix/suffix has been matched.
For the cross-string case, observe that if we choose a substring a from s and b from t such that a+b is a palindrome, then there exists an integer l ≥ 0 so that:
- The first l characters of a equal the reverse of the last l characters of b.
- Depending on which part is longer, the remaining part (either the suffix of a or the prefix of b) must be a palindrome. For every pair of indices i in s and j in t satisfying s[i] == t[j], we “try” to use the characters starting at s[i] and going forward and the characters in t ending at j going backwards as the matching (mirrored) portions. Using two pointers we expand from these positions (while staying in bounds and while the characters match in the “reverse” order). Let l be the maximal matching length. Then there are two ways to add an extra palindromic block:
- Extend the s‐side: if s has extra characters right after the matched portion, then the longest palindrome starting at index i+l (found in lpS) can be appended.
- Extend the t‐side: if t has extra characters before the matched portion (since we are matching from the right), then the longest palindrome ending at index j−l (found in lpT_end) can be used. The candidate answer in these cases is 2*l plus the extra palindrome length. We also keep track of the best pure substring palindrome from either s or t. The maximum over all these cases is our answer.