Imagine you have a string that's like a folded paper - it has two halves that should mirror each other perfectly to form a palindrome. You're given a string s of even length n, and for each query, you can rearrange characters within specific substrings in both halves.
The Challenge: For each query [a, b, c, d], you can:
- Rearrange characters in the left half substring
s[a:b](where0 ≤ a ≤ b < n/2) - Rearrange characters in the right half substring
s[c:d](wheren/2 ≤ c ≤ d < n)
Your goal is to determine if these rearrangements can make the entire string a palindrome. Each query is independent - you start fresh each time!
Example: If s = "abcdef" and query is [0,1,4,5], you can rearrange "ab" and "ef" to potentially make the whole string palindromic.
Input & Output
Visualization
Time & Space Complexity
For each query, we scan the relevant portions of the string once
Only need constant space for character frequency counting (26 letters max)
Constraints
- n == s.length
- 2 ≤ n ≤ 105
- 1 ≤ queries.length ≤ 105
- queries[i].length == 4
- 0 ≤ ai ≤ bi < n / 2
- n / 2 ≤ ci ≤ di < n
- s consists only of lowercase English letters