Can Make Palindrome from Substring - Complete Solution Guide
Can Make Palindrome from Substring is LeetCode problem 1177, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to determine, for each query in a given list of queries, whether a substring of a string `s` can be rearranged into a palindrome after replacing at most `k` characters with any lowercase English letters. Each query consists of a left index `left_i`, a right index `right_i`, and an integer `k_i`. We need to return a boolean array where each element indicates whether the substring `s[left_i...right_i]` can be made a palindrome with at most `k_i` replacements.
Solution Approach
The solution utilizes the prefix XOR technique. It computes a prefix mask array where each element `prefix_masks[i]` represents the XOR of all characters from `s[0]` to `s[i-1]`. For each query, the XOR of `prefix_masks[r+1]` and `prefix_masks[l]` gives a mask representing the substring `s[l...r]`. The number of set bits (bits equal to 1) in this substring mask corresponds to the number of characters that appear an odd number of times in the substring. If half of this number is less than or equal to `k`, then the substring can be made a palindrome with the given number of replacements.
Step-by-Step Algorithm
- Step 1: Create a prefix mask array `prefix_masks` of size `n + 1`, initialized to 0, where `n` is the length of the string `s`.
- Step 2: Iterate through the string `s` from `i = 0` to `n - 1`. For each character `s[i]`, update `prefix_masks[i + 1]` by XORing it with `prefix_masks[i]` and the bit corresponding to the character `s[i]` (calculated as `1 << (ord(s[i]) - ord('a'))`).
- Step 3: Create a boolean array `result` to store the results of the queries.
- Step 4: Iterate through the `queries` array. For each query `[l, r, k]`, calculate the substring mask by XORing `prefix_masks[r + 1]` and `prefix_masks[l]`.
- Step 5: Count the number of set bits (1s) in the `substring_mask`. This represents the number of characters that appear an odd number of times in the substring.
- Step 6: Check if `odd_count / 2 <= k`. If it is, then the substring can be made a palindrome with `k` replacements, so append `true` to the `result` array. Otherwise, append `false`.
- Step 7: Return the `result` array.
Key Insights
- Insight 1: A string can be rearranged into a palindrome if and only if the number of characters appearing an odd number of times is at most 1. Since we can replace characters, we can tolerate more characters appearing an odd number of times, as long as we can 'fix' them with the allowed replacements.
- Insight 2: The number of characters appearing an odd number of times in a substring can be efficiently calculated using bit manipulation with prefix XOR. Each bit in an integer can represent whether a character ('a' to 'z') appears an odd or even number of times. XORing the prefix masks allows us to compute the counts for each substring.
- Insight 3: The number of required replacements to make a string palindrome is half the number of characters that occur an odd number of times (rounded up if needed, but we can handle by integer division since we need only to make sure if the replacements required <= k).
Complexity Analysis
Time Complexity: O(m + n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String, Bit Manipulation, Prefix Sum.
Companies
Asked at: Akuna Capital.