Split Two Strings to Make Palindrome - Problem

You are given two strings a and b of the same length. Choose an index and split both strings at the same index, splitting a into two strings: aprefix and asuffix where a = aprefix + asuffix, and splitting b into two strings: bprefix and bsuffix where b = bprefix + bsuffix. Check if aprefix + bsuffix or bprefix + asuffix forms a palindrome.

When you split a string s into sprefix and ssuffix, either ssuffix or sprefix is allowed to be empty. For example, if s = "abc", then "" + "abc", "a" + "bc", "ab" + "c", and "abc" + "" are valid splits.

Return true if it is possible to form a palindrome string, otherwise return false.

Input & Output

Example 1 — Basic Valid Case
$ Input: a = "x", b = "y"
Output: true
💡 Note: Split at position 0: aprefix="" + bsuffix="y" = "y" (palindrome), or split at position 1: aprefix="x" + bsuffix="" = "x" (palindrome)
Example 2 — Valid Combination
$ Input: a = "xbdef", b = "xecab"
Output: false
💡 Note: No split position creates a palindrome: splitting anywhere produces non-palindromic combinations
Example 3 — Another Valid Case
$ Input: a = "ulacfd", b = "jizalu"
Output: true
💡 Note: Split at position 3: aprefix="ula" + bsuffix="alu" = "ulaalu" (palindrome)

Constraints

  • 1 ≤ a.length, b.length ≤ 105
  • a.length == b.length
  • a and b consist of lowercase English letters

Visualization

Tap to expand
Split Two Strings to Make PalindromeInput:a = "ulacfd"b = "jizalu"Split at position 3:"ula" + "alu""jiz" + "cfd""ulaalu" is a palindrome ✓Output: true
Understanding the Visualization
1
Input
Two strings a and b of equal length
2
Process
Find split position where aprefix+bsuffix or bprefix+asuffix forms palindrome
3
Output
Return true if palindrome possible, false otherwise
Key Takeaway
🎯 Key Insight: Use two pointers to efficiently check if strings can be split to form palindromes by comparing from both ends
Asked in
Facebook 25 Google 20
28.5K Views
Medium Frequency
~25 min Avg. Time
892 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