<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]:
        def cidx(c):
            return ord(c) - ord('a')

        def possible(start, end):
            for i in range(26):
                seg_cnt = cnt[end][i] - (0 if start == 0 else cnt[start - 1][i])
                bseg_cnt = bcnt[end][i] - (0 if start == 0 else bcnt[start - 1][i])
                if seg_cnt != bseg_cnt:
                    return False
            return True

        # #####################################################
        # This will cause TLE if not properly implemented
        # this is copied from 
        # https://leetcode.com/problems/palindrome-rearrangement-queries/solutions/4481115/prefix-sum-and-substring-equal-index/
        def ispali(start, end):
            if pos_len[start] + start > end:
                return True
            return False

        n = len(s)
        hn = n // 2
        cnt = [[0] * 26]
        bcnt = [[0] * 26]
        pali = []
        bpali = [False] * hn
        idiff = []

        for i in range(hn):
            if i > 0:
                cnt.append(cnt[i - 1][:])
                bcnt.append(bcnt[i - 1][:])
            cnt[i][cidx(s[i])] += 1
            bcnt[i][cidx(s[n - i - 1])] += 1
            if i > 0 and pali[i - 1] == False:
                pali.append(False)
            else:
                pali.append(s[i] == s[n - i - 1])

        pos_len = [0] * n
        pre = 0
        # pleae note here is range(hn + 1), not hn because if there's
        # no j that s[j] != s[n - j - 1], then pre_len[0. i, .hn] should 
        # be set to hn - i when j == hn
        for j in range(hn + 1):
            if j == hn or s[j] != s[n - j - 1]:
                for i in range(pre, j):
                    pos_len[i] = j-i
                pre = j+1

        for i in range(hn - 1, -1, -1):
            if i < hn - 1 and bpali[i + 1] == False:
                bpali[i] = False
            else:
                bpali[i] = s[i] == s[n - i - 1]
        
        ans = []
        for a, b, c, d in queries:
            rd = n - c - 1
            rc = n - d - 1
            imin = min(a, b, rc, rd)
            imax = max(a, b, rc, rd)
            if imin > 0 and pali[imin - 1] == False or imax < hn - 1 and bpali[imax + 1] == False:
                ans.append(False)
                continue

            if b < rc or rd < a:
                gap = [a, b, rc, rd]
                gap.sort()
                if gap[2] - gap[1] > 1 and not ispali(gap[1] + 1, gap[2] -1):
                    ans.append(False)
                    continue
                ans.append(possible(a, b) and possible(rc, rd))
            elif rc >= a and rd <= b or a >= rc and b <= rd:
                ans.append(possible(min(a, rc), max(b, rd)))
            elif a < rc and b >= rc or a <= rd and b > rd:
                diff = [0] * 26
                imin, imax = (a, rc - 1) if a < rc else (rd + 1, b)
                isbreak = False
                for i in range(26):
                    diff[i] = cnt[b][i] - (cnt[a - 1][i] if a > 0 else 0) - (bcnt[imax][i] - (bcnt[imin - 1][i] if imin > 0 else 0))
                    if diff[i] < 0:
                        ans.append(False)
                        isbreak = True
                        break
                if isbreak:
                    continue
                bdiff = [0] * 26
                imin, imax = (rc, a - 1) if a > rc else (b + 1, rd)
                for i in range(26):
                    bdiff[i] = bcnt[rd][i] - (bcnt[rc - 1][i] if rc > 0 else 0) - (cnt[imax][i] - (cnt[imin - 1][i] if imin > 0 else 0))
                    if diff[i] < 0:
                        ans.append(False)
                        isbreak = True
                        break         
                if not isbreak:
                    for i in range(26):
                        if diff[i] != bdiff[i]:
                            isbreak = True
                            ans.append(False)
                            break
                    ans.append(not isbreak)
            else:
                print(a, b, c, d)

        return ans


            
            
            
1614