Longest Palindrome After Substring Concatenation II - Problem
Challenge: You are given two strings s and t. Your goal is to create the longest possible palindrome by selecting any substring from s (possibly empty) and any substring from t (possibly empty), then concatenating them in that exact order.

What's a palindrome? A string that reads the same forwards and backwards, like "racecar" or "aba".

Input: Two strings s and t
Output: An integer representing the length of the longest palindrome you can form

Example: If s = "abc" and t = "cba", you could take "ab" from s and "ba" from t to form "abba" (length 4), which is a palindrome!

Input & Output

example_1.py โ€” Basic palindrome formation
$ Input: s = "abc", t = "cba"
โ€บ Output: 6
๐Ÿ’ก Note: We can take the entire string "abc" from s and "cba" from t to form "abccba", which is a palindrome of length 6. This is the maximum possible length.
example_2.py โ€” Partial substring selection
$ Input: s = "ab", t = "ba"
โ€บ Output: 4
๐Ÿ’ก Note: We can take "ab" from s and "ba" from t to form "abba", which is a palindrome of length 4. We could also take "a" from s and "a" from t to get "aa" (length 2), but "abba" is longer.
example_3.py โ€” Edge case with empty substrings
$ Input: s = "aa", t = "bb"
โ€บ Output: 2
๐Ÿ’ก Note: The best we can do is take "aa" from s and empty string from t (or vice versa) to form "aa", which is a palindrome of length 2. Concatenating "aa" + "bb" gives "aabb" which is not a palindrome.

Constraints

  • 0 โ‰ค s.length, t.length โ‰ค 1000
  • s and t consist of lowercase English letters only
  • At least one of s or t must be non-empty
  • The concatenated result must be in the form s_substring + t_substring

Visualization

Tap to expand
Building the Longest Palindrome BridgeSupplier S"abc"Materials: a, b, cab, bc, abcSupplier T"cba"Materials: c, b, acb, ba, cbaSymmetrical BridgeabccbaResult: "abccba" (length 6)Combine materialsโœ“ Perfect Symmetry!Reads same forwards & backwardsLeft supplier materialsRight supplier materialsFinal palindrome bridge
Understanding the Visualization
1
Survey Materials
Examine all available substrings from both supplier s and supplier t
2
Test Combinations
Try different combinations of materials, checking if each forms a symmetrical bridge
3
Optimize with Memory
Remember previous results to avoid testing the same combinations repeatedly
4
Find Maximum
Select the combination that creates the longest symmetrical bridge
Key Takeaway
๐ŸŽฏ Key Insight: Just like building a symmetrical bridge, we need to carefully select materials from both suppliers (substrings from s and t) such that when combined, they form a perfect mirror image. The optimal solution uses dynamic programming to efficiently explore all combinations while remembering previous results.
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 29
42.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen