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] (where 0 ≤ a ≤ b < n/2)
  • Rearrange characters in the right half substring s[c:d] (where n/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: [True]
💡 Note: We can rearrange 'ab' to 'ba' and 'ef' to 'fe', making the string 'bafcfe'. But we need it to be palindromic. Actually, we can rearrange 'ab' and 'ef' such that combined characters 'abef' can form palindromic pairs at positions that would make the whole string palindromic.
example_2.py — Multiple Queries
$ Input: s = "abcdba", queries = [[1,2,4,5]]
Output: [True]
💡 Note: We can rearrange substring 'bc' and 'ba' such that the result forms a palindrome. The characters 'bcba' can be rearranged to form pairs that make the string palindromic.
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.

Visualization

Tap to expand
Flexible AFixed MirrorFlexible BMirror LineA:2B:4C:1✓ Perfect Mirror Possible!Only 1 odd count (C) ≤ 1 allowed
Understanding the Visualization
1
Identify Flexible Zones
Mark which sections of the mirror can have their pieces rearranged
2
Count Glass Pieces
Count each type of glass piece in the flexible zones
3
Check Fixed Reflection
Ensure the fixed middle section already mirrors correctly
4
Validate Pairing
Confirm that pieces can be paired symmetrically (at most one leftover)
Key Takeaway
🎯 Key Insight: A palindrome is possible when character frequencies allow symmetric pairing - at most one character can have an odd count!

Time & Space Complexity

Time Complexity
⏱️
O(q * n)

For each query, we scan the relevant portions of the string once

n
2n
Linear Growth
Space Complexity
O(1)

Only need constant space for character frequency counting (26 letters max)

n
2n
Linear Space

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
Asked in
Google 42 Meta 38 Microsoft 31 Amazon 27
31.2K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen