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: [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
Palindrome Rearrangement Queries INPUT String s (even length) Left Half Right Half a 0 b 1 c 2 d 3 e 4 f 5 Mirror Line Query: [0, 1, 4, 5] Left: s[0:1] "ab" Right: s[4:5] "ef" Input Values s = "abcdef" n = 6 (even) queries = [[0,1,4,5]] ALGORITHM STEPS 1 Identify Mirror Pairs s[i] should match s[n-1-i] 0-5: a!=f 1-4: b!=e 2-3: c!=d All mismatched! 2 Check Query Coverage Verify rearrangeable ranges Left [0,1] covers idx 0,1 Right [4,5] covers idx 4,5 3 Count Characters Compare frequency in ranges Left "abc": a:1 b:1 c:1 Right "def": d:1 e:1 f:1 4 Verify Palindrome Can chars match mirrors? No common chars between halves But query covers ALL positions --> Rearrange freely! FINAL RESULT After Rearrangement f e c d e f Palindrome possible: "fedcdef" (example) Output: [True] Palindrome achievable! Verification - All positions covered - Chars can be swapped - Result: OK Key Insight: The string can become a palindrome if: (1) All mismatched positions are covered by query ranges, (2) The combined character frequencies in corresponding ranges can form mirror pairs. Time: O(n + q*n) with prefix sums | Space: O(n) for character frequency arrays TutorialsPoint - Palindrome Rearrangement Queries | Optimal Solution
Asked in
Google 42 Meta 38 Microsoft 31 Amazon 27
31.3K 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