Can Make Palindrome from Substring Problem
Description
LeetCode Problem 1177.
You are given a string s and array queries where queries[i] = [left_i, right_i, k_i]. We may rearrange the substring s[left_i...right_i] for each query and then choose up to k_i of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false.
Return a boolean array answer where answer[i] is the result of the i^th query queries[i].
Note that each letter is counted individually for replacement, so if, for example s[left_i...right_i] = "aaa", and k_i = 2, we can only replace two of the letters. Also, note that no query modifies the initial string s.
Example :
Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0]: substring = "d", is palidrome.
queries[1]: substring = "bc", is not palidrome.
queries[2]: substring = "abcd", is not palidrome after replacing only 1 character.
queries[3]: substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first rearrange it "bacd" then replace "cd" with "ab".
queries[4]: substring = "abcda", could be changed to "abcba" which is palidrome.
Example 2:
Input: s = "lyb", queries = [[0,1,0],[2,2,1]]
Output: [false,true]
Constraints:
1 <= s.length, queries.length <= 10^50 <= left_i <= right_i < s.length0 <= k_i <= s.lengthsconsists of lowercase English letters.
Solution
Approach: Prefix Parity Bitmasks
Since we may rearrange the substring freely, only the letter counts matter. A multiset of letters can be arranged into a palindrome when at most one letter has an odd count (that one goes in the middle). If odd letters have an odd count, replacing one of them with another odd letter fixes two at once, so odd / 2 (rounded down) replacements are enough, and fewer are not. A query is true exactly when odd / 2 <= k.
So for each query we need to know which letters occur an odd number of times in s[left..right]. Only the parity matters, so store it as a 26-bit mask: parity[i] has bit c set when letter c occurs an odd number of times in the first i characters. Building it is one XOR per character.
The parities of a substring are the difference of two prefixes, and for parities "difference" is XOR: parity[right + 1] ^ parity[left]. Its number of set bits is odd.
For example, in "abcda" the query [0, 3, 1] looks at "abcd": four letters with odd counts, so it needs 2 replacements and k = 1 is not enough.
Complexity: O(n + q) time, with a constant-time popcount per query, and O(n) space for the prefix masks.
class Solution:
def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
# parity[i]: bit c is set when letter c occurs an odd number of times in s[:i].
parity = [0] * (len(s) + 1)
for i, ch in enumerate(s):
parity[i + 1] = parity[i] ^ (1 << (ord(ch) - ord("a")))
answer = []
for left, right, k in queries:
odd = bin(parity[right + 1] ^ parity[left]).count("1") # letters with an odd count
answer.append(odd // 2 <= k)
return answer