HardNo. 2983
Palindrome Rearrangement Queries — LeetCode 2983 Python Solution
- Pattern
- Prefix Sum
- Topics
- Hash Table, String, Prefix Sum
- Reading time
- 10 min
- Source
- leetcode.com
The problem
You are given a 0-indexed string s having an even length n. You are also given a 0-indexed 2D integer array, queries, where queries[i] = [ai, bi, ci, di].
Example
- Input
- s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
- Output
- [true,true]
- Explanation
- In this example, there are two queries:
Python solution
class Solution:
def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
def count(pre: List[List[int]], i: int, j: int) -> List[int]:
return [x - y for x, y in zip(pre[j + 1], pre[i])]
def sub(cnt1: List[int], cnt2: List[int]) -> List[int]:
res = []
for x, y in zip(cnt1, cnt2):
if x - y < 0:
return []
res.append(x - y)
return res
def check(
pre1: List[List[int]], pre2: List[List[int]], a: int, b: int, c: int, d: int
) -> bool:
if diff[a] > 0 or diff[m] - diff[max(b, d) + 1] > 0:
return False
if d <= b:
return count(pre1, a, b) == count(pre2, a, b)
if b < c:
return (
diff[c] - diff[b + 1] == 0
and count(pre1, a, b) == count(pre2, a, b)
and count(pre1, c, d) == count(pre2, c, d)
)
cnt1 = sub(count(pre1, a, b), count(pre2, a, c - 1))
cnt2 = sub(count(pre2, c, d), count(pre1, b + 1, d))
return bool(cnt1) and bool(cnt2) and cnt1 == cnt2
n = len(s)
m = n // 2
t = s[m:][::-1]
s = s[:m]
pre1 = [[0] * 26 for _ in range(m + 1)]
pre2 = [[0] * 26 for _ in range(m + 1)]
diff = [0] * (m + 1)
for i, (c1, c2) in enumerate(zip(s, t), 1):
pre1[i] = pre1[i - 1][:]
pre2[i] = pre2[i - 1][:]
pre1[i][ord(c1) - ord("a")] += 1
pre2[i][ord(c2) - ord("a")] += 1
diff[i] = diff[i - 1] + int(c1 != c2)
ans = []
for a, b, c, d in queries:
c, d = n - 1 - d, n - 1 - c
ok = (
check(pre1, pre2, a, b, c, d)
if a <= c
else check(pre2, pre1, c, d, a, b)
)
ans.append(ok)
return ansComplexity
- Time
- O((n + q) × |Σ|)
- Space · auxiliary
- O(n × |Σ|)
Pattern: Prefix Sum
Precompute running totals once so any range query becomes a single subtraction. LeetCode 2983. Palindrome Rearrangement Queries is filed here because LeetCode tags it Prefix Sum, which is the vocabulary this hub collects.
The prefix sum guide has the Python template for the pattern and the 157 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1177 | Can Make Palindrome from Substring | Medium |
| 1371 | Find the Longest Substring Containing Vowels in Even Counts | Medium |
| 1737 | Change Minimum Characters to Satisfy One of Three Conditions | Medium |
| 1915 | Number of Wonderful Substrings | Medium |
| 1930 | Unique Length-3 Palindromic Subsequences | Medium |
| 2947 | Count Beautiful Substrings I | Medium |
Frequently asked questions
- How hard is LeetCode 2983. Palindrome Rearrangement Queries?
LeetCode 2983. Palindrome Rearrangement Queries is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2983. Palindrome Rearrangement Queries?
The Python solution on this page runs in O((n + q) × |Σ|).
- What is the space complexity of LeetCode 2983. Palindrome Rearrangement Queries?
The Python solution on this page uses O(n × |Σ|) auxiliary space.
- What topics does LeetCode 2983. Palindrome Rearrangement Queries cover?
LeetCode 2983. Palindrome Rearrangement Queries is tagged Hash Table, String and Prefix Sum on LeetCode.