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.

Notice that&nbsp;x + y denotes the concatenation of strings x and y.

&nbsp;
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:
aprefix = "", asuffix = "x"
bprefix = "", bsuffix = "y"
Then, aprefix + bsuffix = "" + "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:
aprefix = "ula", asuffix = "cfd"
bprefix = "jiz", bsuffix = "alu"
Then, aprefix + bsuffix = "ula" + "alu" = "ulaalu", which is a palindrome.


&nbsp;
Constraints:


	1 &lt;= a.length, b.length &lt;= 105
	a.length == b.length
	a and b consist of lowercase English letters


class Solution:
    def checkPalindromeFormation(self, a: str, b: str) -> bool:
        def pal(x):
            return x == x[::-1]
        if pal(a) or pal(b): return True
        # either grow from inside to outside, or vice versa
        ina = len(a)-1
        inb = 0
        outa = 0
        outb = len(b)-1

        while a[ina] == b[inb]:
            ina -= 1
            inb += 1
            if ina <= inb:
                return True # short circuit found break point
        # jump into each string now!?
        # is a or b a palindrome in this portion from inb to ina
        if pal(a[inb:ina+1]) or pal(b[inb:ina+1]):
            return True # either one is breakpoint, so check remainder is palindrome

        while a[outa] == b[outb]:
            outa += 1
            outb -= 1
            if outa >= outb:
                return True
        if pal(a[outa:outb+1]) or pal(b[outa:outb+1]):
            return True # either one is breakpoint, so check remainder

        return False
class Solution {
    public boolean checkPalindromeFormation(String a, String b) {
        return split(a, b) || split(b, a);
    }
    
    private boolean split(String a, String b) {
        int left = 0, right = a.length() - 1;
        while (left < right && a.charAt(left) == b.charAt(right)) {
            left++;
            right--;
        }
        if (left >= right) return true;
        return isPalindrome(a, left, right) || isPalindrome(b, left, right);
    }
    
    private boolean isPalindrome(String s, int left, int right) {
        while (left <= right) {
            if (s.charAt(left) != s.charAt(right)) return false;
            left++;
            right--;
        }
        return true;
    }
}
// samll trick: for plaindrome question always try to follow concept that if corners are equal we need to only work
// on middle string to check whether it is also palindrome, instead of check complete strings(both given strings).
class Solution {
public:
    bool ispalind(string x, int i, int j){
        while(i<j){
            if(x[i]!=x[j]) return false;
            i++;
            j--;
        }
        return true;
    }
    bool checkpositions(string a, string b){
        int i=0,j=b.size()-1;
        while(i<j){
            if(a[i]!=b[j]) break;
            i++;
            j--;
        }
         /*
          left cut
          //agar same hote toh
            "ulacfd" ul.zalu //to check for palindrome : za(from b)
            "jizalu" ji.acfd //to check for palindrome : ac(from a)
        */
        /*
         right cut
         //agar samee hote toh
            "ulacfd" jiza.fd //to check for palindrome : za(from b)
            "jizalu" ulac.lu //to check for palindrome : ac(from a)
        */
        return ispalind(a,i,j) || ispalind(b,i,j);
    }
    bool checkPalindromeFormation(string a, string b) {
              //cut one(from left) //cut two(from right)
        return checkpositions(a,b)||checkpositions(b,a);
    }
};
var checkPalindromeFormation = function(a, b) {
    function isPal(str, l, r) {
        while (l < r) {
            if (str[l] === str[r]) l++, r--;
            else return false;
        } return true;
    }
    
    // aprefix + bsuffix
    let l = 0, r = b.length - 1;
    while (l < r && a[l] === b[r]) l++, r--;
    if (isPal(a, l, r) || isPal(b, l, r)) return true;
    
    // bprefix + asuffix
    l = 0, r = a.length - 1;
    while (l < r && b[l] === a[r]) l++, r--;
    if (isPal(a, l, r) || isPal(b, l, r)) return true;
    
    return false;
};
Split Two Strings to Make Palindrome