Problem statement

https://binarysearch.com/problems/Cut-Palindrome/

Solution

Equal to Leetcode 1616 Split Two Strings to Make Palindrome.

Complexity

It is O(n) for time and space.

Code

from os.path import commonprefix

class Solution:
    def solve(self, a, b):
        n = len(a)
        t1 = len(commonprefix([a, b[::-1]]))
        t2 = len(commonprefix([b, a[::-1]]))
        t3 = max(t1, t2)
        if t3*2 >= n: return True
        if a == a[::-1] or b == b[::-1]: return True
        if t3 > 0 and a[t3:-t3] == a[t3:-t3][::-1]: return True
        if t3 > 0 and b[t3:-t3] == b[t3:-t3][::-1]: return True
        return False