Problem Description
Given an even‑length string s and a set of queries, each query allows you to rearrange two specific substrings – one in the left half and one in the right half of s. For a query [a, b, c, d], you may arbitrarily reorder the characters in s[a:b] (with 0 ≤ a ≤ b < n/2) and in s[c:d] (with n/2 ≤ c ≤ d < n). The task is to decide whether, after these independent rearrangements, it is possible to convert s into a palindrome.
Key Insights
- A palindrome of an even‑length string requires that for every index i in [0, n/2), the character s[i] equals s[n–1–i].
- Only portions of the left and right halves are “modifiable.” The characters in the other (fixed) positions remain in place and must already satisfy the mirror property.
- If both characters in a mirror pair are fixed then they must already match.
- When one character of a mirror pair is fixed yet its partner is in an allowed (modifiable) segment, then that allowed segment must “supply” the needed character.
- For mirror pairs where both positions are modifiable, the multiset of characters available in the allowed regions from the left and right halves must be equal (so that they can be rearranged into identical halves).
- Pre‐computing prefix frequency arrays (for each half) and a “mismatch” array for fixed (non‑modifiable) positions allows each query to be answered in O(26) time, i.e. constant time per query.
Space and Time Complexity
Time Complexity: O(n + Q * 26) = O(n + Q) where n is the length of s and Q is the number of queries.
Space Complexity: O(n * 26) for the prefix frequency arrays (which is acceptable given the constant 26).
Solution
The idea is to “split” the string into two halves. For each query, we know exactly which segments in the left half (indices [a,b]) and right half (indices [c,d]) can be rearranged arbitrarily.
For every mirror pair (i, n–1–i) with i in the left half, there are three cases:
- Both positions are fixed. In this case the already‑present characters must match. We precompute a “mismatch” prefix so we can quickly count mismatches in any fixed segment.
- One position is fixed and the other modifiable. Then the modifiable part must supply the character from its fixed partner. In effect, we “demand” a certain frequency for that letter.
- Both positions are modifiable. Their characters are pooled in the allowed segments from the left and right parts. After subtracting the characters “used” in the fixed–modification pairs, the residual multiset in the modifiable left must equal that in the modifiable right.
To support fast queries we precompute:
– For the left half: a prefix frequency array (for a–z) over indices 0 to n/2–1.
– For the right half: a prefix frequency array (for a–z) over indices n/2 to n–1.
– A “mismatch” prefix array for mirror pairs. Then for each query we: • Identify the “modifiable” indices in the left half (given directly by [a,b]) and in the right half through their mirror mapping. When an index i in the left half has its mirror (n–1–i) in [c,d], then that left index belongs to the modifiable set from the right side (compute the mirror interval as [n–1–d, n–1–c]). • Use the prefix frequency arrays to extract counts fast for any subinterval. • Compute frequency “demands” for pairs where one side is fixed and the other is modifiable. • Check that the fixed–fixed pairs already match. • Finally, check that after meeting demands the remaining characters in the left and right allowed segments have exactly the same multiset. If all these conditions hold the answer for that query is true; otherwise false.
Code Solutions
Below are code examples in Python, JavaScript, C++, and Java. Each solution uses clear variable names and inline comments.