<p>You are given a <strong>0-indexed</strong> string <code>s</code> having an <strong>even</strong> length <code>n</code>.</p>

<p>You are also given a <strong>0-indexed</strong> 2D integer array, <code>queries</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, d<sub>i</sub>]</code>.</p>

<p>For each query <code>i</code>, you are allowed to perform the following operations:</p>

<ul>
	<li>Rearrange the characters within the <strong>substring</strong> <code>s[a<sub>i</sub>:b<sub>i</sub>]</code>, where <code>0 &lt;= a<sub>i</sub> &lt;= b<sub>i</sub> &lt; n / 2</code>.</li>
	<li>Rearrange the characters within the <strong>substring</strong> <code>s[c<sub>i</sub>:d<sub>i</sub>]</code>, where <code>n / 2 &lt;= c<sub>i</sub> &lt;= d<sub>i</sub> &lt; n</code>.</li>
</ul>

<p>For each query, your task is to determine whether it is possible to make <code>s</code> a <strong>palindrome</strong> by performing the operations.</p>

<p>Each query is answered <strong>independently</strong> of the others.</p>

<p>Return <em>a <strong>0-indexed</strong> array </em><code>answer</code><em>, where </em><code>answer[i] == true</code><em> if it is possible to make </em><code>s</code><em> a palindrome by performing operations specified by the </em><code>i<sup>th</sup></code><em> query, and </em><code>false</code><em> otherwise.</em></p>

<ul>
	<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
	<li><code>s[x:y]</code> represents the substring consisting of characters from the index <code>x</code> to index <code>y</code> in <code>s</code>, <strong>both inclusive</strong>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcabc&quot;, queries = [[1,1,3,5],[0,2,5,5]]
<strong>Output:</strong> [true,true]
<strong>Explanation:</strong> In this example, there are two queries:
In the first query:
- a<sub>0</sub> = 1, b<sub>0</sub> = 1, c<sub>0</sub> = 3, d<sub>0</sub> = 5.
- So, you are allowed to rearrange s[1:1] =&gt; a<u>b</u>cabc and s[3:5] =&gt; abc<u>abc</u>.
- To make s a palindrome, s[3:5] can be rearranged to become =&gt; abc<u>cba</u>.
- Now, s is a palindrome. So, answer[0] = true.
In the second query:
- a<sub>1</sub> = 0, b<sub>1</sub> = 2, c<sub>1</sub> = 5, d<sub>1</sub> = 5.
- So, you are allowed to rearrange s[0:2] =&gt; <u>abc</u>abc and s[5:5] =&gt; abcab<u>c</u>.
- To make s a palindrome, s[0:2] can be rearranged to become =&gt; <u>cba</u>abc.
- Now, s is a palindrome. So, answer[1] = true.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abbcdecbba&quot;, queries = [[0,2,7,9]]
<strong>Output:</strong> [false]
<strong>Explanation:</strong> In this example, there is only one query.
a<sub>0</sub> = 0, b<sub>0</sub> = 2, c<sub>0</sub> = 7, d<sub>0</sub> = 9.
So, you are allowed to rearrange s[0:2] =&gt; <u>abb</u>cdecbba and s[7:9] =&gt; abbcdec<u>bba</u>.
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
So, answer[0] = false.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;acbcab&quot;, queries = [[1,2,4,5]]
<strong>Output:</strong> [true]
<strong>Explanation: </strong>In this example, there is only one query.
a<sub>0</sub> = 1, b<sub>0</sub> = 2, c<sub>0</sub> = 4, d<sub>0</sub> = 5.
So, you are allowed to rearrange s[1:2] =&gt; a<u>cb</u>cab and s[4:5] =&gt; acbc<u>ab</u>.
To make s a palindrome s[1:2] can be rearranged to become a<u>bc</u>cab.
Then, s[4:5] can be rearranged to become abcc<u>ba</u>.
Now, s is a palindrome. So, answer[0] = true.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
	<li><code>queries[i].length == 4</code></li>
	<li><code>a<sub>i</sub> == queries[i][0], b<sub>i</sub> == queries[i][1]</code></li>
	<li><code>c<sub>i</sub> == queries[i][2], d<sub>i</sub> == queries[i][3]</code></li>
	<li><code>0 &lt;= a<sub>i</sub> &lt;= b<sub>i</sub> &lt; n / 2</code></li>
	<li><code>n / 2 &lt;= c<sub>i</sub> &lt;= d<sub>i</sub> &lt; n </code></li>
	<li><code>n</code> is even.</li>
	<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

class Solution:
    def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
        n = len(s)
        m = n // 2
        # split in 2 and reverse second
        s1, s2 = s[:m], s[m:][::-1]
        
        arr = []
        for i in range(m):
            if s1[i] != s2[i]:
                arr.append(i)
        
        # store prefix char count before pos 'i'. 
        count1 = [[0]*26]
        count2 = [[0]*26]
        orda = ord('a')
        for i in range(m):
            count1.append(count1[-1][::])
            count2.append(count2[-1][::])
            pos1 = ord(s1[i]) - orda
            pos2 = ord(s2[i]) - orda
            count1[-1][pos1] += 1
            count2[-1][pos2] += 1
        
        # no differences. Return True
        if not arr:
            return [True]*len(queries)
        
        # checks that chars in range [a..b] are same in both s1 & s2
        def checkChars(a, b):
            for i in range(26):
                if count1[b+1][i]-count1[a][i] != count2[b+1][i]-count2[a][i]:
                    return False
            return True

        def checkIntersectChars(a,b,c,d):
            c1, c2 = count1, count2
            # change order [a..b] to start first
            if c < a:
                a, b, c, d = c, d, a, b
                c1, c2 = c2, c1
            # [a..b] covers everything
            if b >= d:
                return checkChars(a,b)
            # [c..d] covers everything
            if c <= a:
                return checkChars(c,d)
            
            # checking chars in full range
            if not checkChars(min(a,c), max(b,d)):
                return False
            
            # checking each separate range to see that it has enough chars.
            # s1[a..b] should have enough chars to build s2[a..(c-1)]
            # same on the opposite side. s2[c..d] should have enough chars to build s1[(b+1)..d]
            # chars in the intersecting area are covered by above check.
            for i in range(26):
                if c1[b+1][i]-c1[a][i] < c2[c][i]-c2[a][i]:
                    return False
                if c2[d+1][i]-c2[c][i] < c1[d+1][i]-c1[b+1][i]:
                    return False
            return True
                

        # check that ranges [a..b] and [c..d] include all chars that are different in s1 & s2
        def checkCover(a,b,c,d):
            if c < a:
                a, b, c, d = c, d, a, b
            # range intersection. we can join the ranges.
            if c <= b:
                # check edges
                if min(a, c) > arr[0] or max(b, d) < arr[-1]:
                    return False
                else:
                    return True
                
            # check edges
            if a > arr[0] or d < arr[-1]:
                return False
            # check between ranges
            pos = bisect.bisect_right(arr,b)
            if pos < len(arr) and arr[pos] < c:
                return False
            return True
                
        res = []
        for i, [a,b,c,d] in enumerate(queries):
            # reverting order. if it's last 2 chars [n-2, n-1] it becomes first 2 in reverse [0,1]
            c, d = n-d-1, n-c-1, 
            
            if not checkCover(a,b,c,d):
                res.append(False)
                continue

            # no intersection between ranges
            if b < c or d < a:
                res.append(checkChars(a,b) and checkChars(c,d))
                continue
            
            # intersection
            res.append(checkIntersectChars(a,b,c,d))

        return res
1273