Split Two Strings to Make Palindrome Problem
Description
LeetCode Problem 1616.
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: a_prefix and a_suffix where a = a_prefix + a_suffix, and splitting b into two strings: b_prefix and b_suffix where b = b_prefix + b_suffix. Check if a_prefix + b_suffix or b_prefix + a_suffix forms a palindrome.
When you split a string s into s_prefix and s_suffix, either s_suffix or s_prefix 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.
Notice that x + y denotes the concatenation of strings x and y.
Example 1:
Input: a = "x", b = "y"
Output: true
Explaination: If either a or b are palindromes the answer is true since you can split in the following way:
a_prefix = "", a_suffix = "x"
b_prefix = "", b_suffix = "y"
Then, a_prefix + b_suffix = "" + "y" = "y", which is a palindrome.
Example 2:
Input: a = "xbdef", b = "xecab"
Output: false
Example 3:
Input: a = "ulacfd", b = "jizalu"
Output: true
Explaination: Split them at index 3:
a_prefix = "ula", a_suffix = "cfd"
b_prefix = "jiz", b_suffix = "alu"
Then, a_prefix + b_suffix = "ula" + "alu" = "ulaalu", which is a palindrome.
Constraints:
1 <= a.length, b.length <= 10^5a.length == b.lengthaandbconsist of lowercase English letters
Solution
Approach: Two Pointers From Both Ends
Checking every split index and building the joined string each time would cost O(n2). Instead, look at how a palindrome a_prefix + b_suffix must be shaped.
Its first characters come from a and its last characters come from b, so a[0] must equal b[n-1], a[1] must equal b[n-2], and so on. Put a pointer i at the start of a and a pointer j at the end of b, and move them inward while a[i] == b[j]. Matching as far as possible never hurts: every matched pair can sit in the outer part of the palindrome.
When the pointers stop, the middle part [i, j] still has to be a palindrome on its own, and it must come from a single string, because the split falls inside that middle part. So the answer is yes if a[i..j] or b[i..j] is a palindrome (or the pointers already crossed, which counts as an empty middle).
That covers a_prefix + b_suffix. For b_prefix + a_suffix, run the same check with a and b swapped.
The tricky case is the middle: check it in both strings, not only in a. With a = "xabcq" and b = "berex", the ends match only for "x" / "x"; the middle of a is "abc", which fails, but the middle of b is "ere", and splitting after the first character gives "x" + "erex" = "xerex".
Complexity: O(n) time, since each pointer walk is linear, and O(1) extra space.
class Solution:
def checkPalindromeFormation(self, a: str, b: str) -> bool:
def is_palindrome(s: str, i: int, j: int) -> bool:
while i < j and s[i] == s[j]:
i, j = i + 1, j - 1
return i >= j
def check(a: str, b: str) -> bool:
# Match a's prefix against b's suffix as far as possible.
i, j = 0, len(b) - 1
while i < j and a[i] == b[j]:
i, j = i + 1, j - 1
# The middle a[i..j] or b[i..j] must be a palindrome by itself.
return is_palindrome(a, i, j) or is_palindrome(b, i, j)
return check(a, b) or check(b, a)