↓Skip to main content
  1. LeetCode/

LeetCode 1616: Split Two Strings to Make Palindrome

·3 mins· ·
LeetCode Medium Two-Pointers String Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-24

  • Submissions: WA → TLE ×2 → WA → AC → WA → AC

Approach
#

Split a and b at the same index and check whether a prefix + b suffix or b prefix + a suffix is a palindrome.

For a prefix + b suffix, use converging pointers: the left pointer walks right on a and the right pointer walks left on b, continuing while a[left] == b[right]. This outer part can always be formed by “a’s prefix matched with b’s suffix”.

When they stop, the unmatched middle [left, right] must come entirely from one string (the split point falls inside it), so either a[left..right] or b[left..right] must itself be a palindrome.

The other direction, b prefix + a suffix, is checked the same way.

My Attempts
#

VersionProblem
Attempt 1, WAChecked only a prefix + b suffix, with split points only up to n - 1. For a = "aa", b = "ab", taking all of a is a palindrome, so the answer is True; this version returned False
Two TLEsAdded the other direction and split points 0..n, but rebuilt and checked a string for every split: O(n^2)
Attempt 4, WASwitched to greedy matching plus a middle check, but is_palindrome used i -= 1 and j += 1, moving outward and going out of range
Attempt 6, WAThe last line was check(a, b) or check(a, b); the second should be check(b, a). For a = "aaab", b = "babb", b’s prefix "ba" plus a’s suffix "ab" gives "baab", so the answer is True; checking one direction only returned False

Solution
#

class Solution:
    def checkPalindromeFormation(self, a: str, b: str) -> bool:
        def is_palindrome(s: str, i: int, j: int) -> bool:
            while i < j:
                if s[i] != s[j]:
                    return False
                i += 1
                j -= 1
            return True

        def check(x: str, y: str) -> bool:            # x's prefix + y's suffix
            left, right = 0, len(x) - 1
            while left < right and x[left] == y[right]:
                left += 1
                right -= 1
            return is_palindrome(x, left, right) or is_palindrome(y, left, right)

        return check(a, b) or check(b, a)

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Checking only one direction.
  • Rebuilding a string for every split point: O(n^2).
  • Moving the palindrome-check pointers outward.
  • Calling the same function twice without swapping the arguments.

Interview Explanation
#

For a’s prefix plus b’s suffix, I move converging pointers from the outside in while a[left] equals b[right]. Where they stop, the remaining middle must come entirely from one string, so I check whether it is a palindrome within a or within b. The other direction swaps a and b. Time O(n).

Related

LeetCode 1750: Minimum Length of String After Deleting Similar Ends
·3 mins
LeetCode Medium Two-Pointers String
LeetCode 1750 note: when both ends match, each side eats its whole run of that character; a string of one repeated character can be deleted entirely
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·2 mins
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 note: pair each word with its reverse on both sides; self-palindromes also pair with each other, and only one leftover can sit in the middle
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 402: Remove K Digits
·2 mins
LeetCode Medium String Stack Greedy Monotonic-Stack
LeetCode 402 note: a monotonically increasing stack; while deletions remain, pop any top larger than the current digit, then strip leading zeros
LeetCode 151: Reverse Words in a String
·1 min
LeetCode Medium Two-Pointers String
LeetCode 151 note: split() with no argument treats runs of spaces as one separator and drops leading and trailing spaces; reverse and join with single spaces