<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]:

        mismatches = []

        for i in range(len(s) // 2):
            if s[i] != s[-1-i]:
                mismatches.append(i)
        

        if len(mismatches) == 0:
            return [True] * len(queries)

        cc = [{}]
        for c in s:
            counts = {k: v for k, v in cc[-1].items()}
            counts[c] = counts.get(c, 0) + 1
            cc.append(counts)

        def region_cc(lo, hi):
            if hi < lo:
                return {}
            cc_reg = {k: v for k, v in cc[hi+1].items()}
            for cr, cnt in cc[lo].items():
                cc_reg[cr] -= cnt
            return cc_reg

        def cc_identical(cc1, cc2):
            for i in range(26):
                crc = chr(ord('a') + i)
                if cc1.get(crc, 0) != cc2.get(crc, 0):
                    return False
            return True


        def region_total_match(lo, hi):
            cc_left = region_cc(lo, hi)
            lo_m = len(s) - 1 - lo
            hi_m = len(s) - 1 - hi
            cc_right = region_cc(hi_m, lo_m)
            
            return cc_identical(cc_left, cc_right)


        if not cc_identical(
            region_cc(0, len(s) // 2 - 1),
            region_cc(len(s) // 2, len(s) - 1)
        ):
            return [False] * len(queries)
        
        ans = []
        for a, b, c, d in queries:
            if a <= mismatches[0] and b >= mismatches[-1]:
                ans.append(True)
                continue
            cm = len(s) - 1 - c
            dm = len(s) - 1 - d
            if dm <= mismatches[0] and cm >= mismatches[-1]:
                ans.append(True)
                continue
            
            if min(a, dm) > mismatches[0] or max(b, cm) < mismatches[-1]:
                ans.append(False)
                continue
            
            am = len(s) - 1 - a
            bm = len(s) - 1 - b

            # print(f"a b c d {a} {b} {c} {d}")
            # print(f"am bm cm dm {am} {bm} {cm} {dm}")
            
            if b < dm or cm < a:
                regions = [(a, b), (dm, cm)]
                regions.sort()

                (x, y), (p, q) = regions
                ymi = bisect_left(mismatches, y+1)
                if ymi < len(mismatches) and (mismatches[ymi] < p or mismatches[ymi] > q):
                    ans.append(False)
                    continue

                ans.append(region_total_match(x, y) and region_total_match(p, q))
            
                

            elif (dm >= a and cm <= b) or (a >= dm and b <= cm):
                ans.append(region_total_match(min(a, dm), max(b, cm)))

            else:
                ab_cc = region_cc(a, b)
                if dm < a or cm < b:
                    # print("dm < a or cm < b")
                    right_immobile_cc = region_cc(bm, c-1)
                    left_immobile_cc = region_cc(dm, a-1)
                else:
                    # print("not that")
                    right_immobile_cc = region_cc(d+1, am)
                    left_immobile_cc = region_cc(b+1, cm)
                cd_cc = region_cc(c, d)

                curr = True

                # print(f"ab_cc {ab_cc}")
                # print(f"cd_cc {cd_cc}")
                # print(f"left_immobile_cc {left_immobile_cc}")
                # print(f"right_immobile_cc {right_immobile_cc}")


                for crc, cnt in ab_cc.items():
                    rim = right_immobile_cc.get(crc, 0)
                    if rim > cnt:
                        curr = False
                        break
                    else:
                        cnt -= rim
                    if cd_cc.get(crc, 0) < cnt:
                        # print("can't deduct from cd_cc")
                        curr = False
                        break
                    else:
                        cd_cc[crc] -= cnt

                # print("after subtractions")
                # print(f"ab_cc {ab_cc}")
                # print(f"cd_cc {cd_cc}")
                # print(f"left_immobile_cc {left_immobile_cc}")
                # print(f"right_immobile_cc {right_immobile_cc}")
                                                    
                if curr and not cc_identical(left_immobile_cc, cd_cc):
                    # print("left immobile not identical")
                    curr = False
                
                ans.append(curr)
        
        return ans



            

1404