Palindrome Rearrangement Queries - Problem
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
example_1.py — Basic Palindrome Check
$
Input:
s = "abcdef", queries = [[0,1,4,5]]
›
Output:
[False]
💡 Note:
We can rearrange 'ab' and 'ef' to get characters 'abef', but the fixed middle characters 'cd' at positions (2,3) don't match, so the string cannot be made palindromic.
example_2.py — Multiple Queries
$
Input:
s = "abcdba", queries = [[1,2,4,5]]
›
Output:
[False]
💡 Note:
We can rearrange 'bc' and 'ba' to get characters 'bcba'. For a palindrome, we need pairs of characters, but we have 2 b's, 1 c, and 1 a. Since we have 2 characters with odd frequencies and the total length is even, we cannot form a palindrome.
example_3.py — Impossible Case
$
Input:
s = "abcdef", queries = [[0,0,5,5]]
›
Output:
[False]
💡 Note:
We can only rearrange 'a' and 'f', but the middle part 'bcde' cannot form a palindrome with the fixed characters, so it's impossible.
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
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code