<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]:
        # we can split our string down the middle abc|abc, and we need to know from the inwards going out if regions are the exact same
        n = len(s)
        
        HALF_LEFT = int((n/2)-1)
        
        sLeft = s[0:HALF_LEFT + 1]
        sRight = s[HALF_LEFT + 1:][::-1]
        
        # find the last start that is the same, in a region where we can change neither char
        startI = None
        for i in range(len(sLeft)):
            if sLeft[i] != sRight[i]:
                startI = i - 1
                break
        if startI == None:
            startI = len(sLeft) - 1
            
        # the earliest index that is the same, starting from the end
        endI = None
        for i in range(len(sLeft) - 1, -1, -1):
            if sLeft[i] != sRight[i]:
                endI = i + 1
                break
        if endI == None:
            endI = 0
            
        abc=list(set(s))
        pfLeft = []
        initCounts = { char : 0 for char in abc }
        for char in sLeft:
            initCounts[char] += 1
            pfLeft.append(initCounts.copy())
        pfRight = []
        initCounts = { char : 0 for char in abc }
        for char in sRight:
            initCounts[char] += 1
            pfRight.append(initCounts.copy())
        
        def query(l, r, prefix):
            if l == 0:
                return prefix[r].copy()
            return {
                char : prefix[r][char] - prefix[l - 1][char] for char in abc
            }
                        
        res = []

        pfMismatch = []
        curr = 0
        for i in range(len(sLeft)):
            curr += sLeft[i] != sRight[i]
            pfMismatch.append(curr)
        
        @cache # not needed but improves a test case
        def queryMismatches(l, r):
            if l == 0:
                return pfMismatch[r]
            return pfMismatch[r] - pfMismatch[l - 1]
        
        for a, b, cOrig, dOrig in queries:
            c = n - dOrig - 1
            d = n - cOrig - 1
            # we fail if the unchangeable prefixes are not the same
            bottleneckStartUnchagenable = min(a, c) - 1            
            if bottleneckStartUnchagenable > startI:
                res.append(False)
                continue
                
            bottleneckEndUnchangeable = max(b, d) + 1
            if bottleneckEndUnchangeable < endI:
                res.append(False)
                continue
                
            # (a b) c d | a b (c d)
            # right edge for one changing zone is 1 (b)
            # shared region is (2, 1) which is inverted
            # right exclusive is (2, 3)
                
            # find the right edge of the region that can only be changed by one zone, or -1 if both zones can
            # rightEdge = max(a, c) - 1
            rightEdge = min(b, d, max(a, c) - 1)
            leftExclusive = (min(a, c), rightEdge) # can be (0,-1) if nothing
            
            # find the region both are in
            changeBoth = (max(a, c), min(b, d)) # can be inverted
            
            rightExclusive = (
                max(
                    min(b, d) + 1,
                    a,
                    c
                ),
                max(b, d)) # can be (n, n-1) if there is no right exclusive
            
            leftChangeable = query(a, b, pfLeft)
            rightChangeable = query(c, d, pfRight)

            # remove the left exclusive part from the flexible pool
            if leftExclusive[0] <= leftExclusive[1]:
                leftExclusiveCounts = query(leftExclusive[0], leftExclusive[1], pfLeft if c < a else pfRight)
                for char in abc:
                    (rightChangeable if c < a else leftChangeable)[char] -= leftExclusiveCounts[char]
            
            # remove the right exclusive part
            if rightExclusive[0] <= rightExclusive[1]:
                rightExclusiveCounts = query(rightExclusive[0], rightExclusive[1], pfRight if b > d else pfLeft)
                for char in abc:
                    (leftChangeable if b > d else rightChangeable)[char] -= rightExclusiveCounts[char]
            
            if not leftChangeable == rightChangeable:
                res.append(False)
                continue
            
            if any(leftChangeable[char] < 0 for char in abc):
                res.append(False)
                continue
                
            neitherRange = (min(b, d) + 1, max(a, c) - 1)
            if neitherRange[0] <= neitherRange[1]:
                if queryMismatches(*neitherRange):
                    res.append(False)
                    continue
            
            res.append(True)

        return res
                
            
            
            
            
            
            
            
            
            
1668