Split Two Strings to Make Palindrome - Problem

You are given two strings a and b of the same length. Your task is to find if there's a way to split both strings at the same index and mix their parts to create a palindrome.

Here's how it works:

  • Choose any index i (can be 0 to n, where n is the string length)
  • Split string a into a_prefix (first i characters) and a_suffix (remaining characters)
  • Split string b into b_prefix (first i characters) and b_suffix (remaining characters)
  • Check if either a_prefix + b_suffix or b_prefix + a_suffix forms a palindrome

Important: Either part of a split can be empty. For example, splitting "abc" can give you "" + "abc", "a" + "bc", "ab" + "c", or "abc" + "".

Return true if it's possible to form a palindrome, otherwise return false.

Input & Output

example_1.py โ€” Basic Case
$ Input: a = "x", b = "y"
โ€บ Output: true
๐Ÿ’ก Note: We can split at position 0: "" + "x" and "" + "y" gives us "x" and "y". Or split at position 1: "x" + "" and "y" + "" gives us "x" and "y". Both "x" and "y" are single characters, so they're palindromes.
example_2.py โ€” Found Palindrome
$ Input: a = "ulacfd", b = "jizalu"
โ€บ Output: true
๐Ÿ’ก Note: Split at position 2: a_prefix="ul", b_suffix="zalu" โ†’ "ul" + "zalu" = "ulzalu" which is a palindrome (reads same forwards and backwards).
example_3.py โ€” No Palindrome Possible
$ Input: a = "xbdef", b = "xecab"
โ€บ Output: false
๐Ÿ’ก Note: No matter where we split, neither a_prefix + b_suffix nor b_prefix + a_suffix forms a palindrome. We've tried all positions 0-5 and none work.

Visualization

Tap to expand
๐Ÿงฌ DNA Strand Palindrome FormationOriginal DNA Strands:Strand A: U-L-A-C-F-DStrand B: J-I-Z-A-L-UCut at position 2:Aโ‚: U-L | A-C-F-DBโ‚: J-I | Z-A-L-Uโœ‚๏ธRecombine Aโ‚_prefix + Bโ‚_suffix:Result: U-L-Z-A-L-U๐Ÿ”ฌPalindrome detected!๐Ÿ”ฌReading forward: U-L-Z-A-L-UReading backward: U-L-A-Z-L-Uโœ“ Same sequence - stable configuration!Alternative: Bโ‚_prefix + Aโ‚_suffix:Result: J-I-A-C-F-DNot palindromic - unstable๐ŸŽฏ Key Insight: Most palindrome checking happens at the edges - efficient two-pointer scanning!
Understanding the Visualization
1
Examine the Strands
We have two DNA strands of equal length that we need to cut and recombine
2
Try Different Cut Points
Like a scientist testing different cutting positions with molecular scissors
3
Swap and Test
For each cut, swap the tails and check if the result is palindromic using dual microscopes
4
Find Stable Configuration
A palindromic DNA sequence is found - this would be stable and valuable for research!
Key Takeaway
๐ŸŽฏ Key Insight: We can efficiently check for palindromic combinations by using two pointers from the edges inward, avoiding expensive string concatenations while still testing all possible split points.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

O(n) split positions ร— O(n) palindrome check, but with better constants due to early termination

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using pointers, no additional string storage needed

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค a.length, b.length โ‰ค 105
  • a.length == b.length
  • a and b consist of lowercase English letters only
  • Both strings must have the same length
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
78.2K Views
Medium-High Frequency
~18 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