Can Make Palindrome from Substring - Problem
Can Make Palindrome from Substring is a fascinating problem that combines string manipulation, bit manipulation, and palindrome properties.
You're given a string
Key Operations Allowed:
1. Rearrange characters in the substring in any order
2. Replace up to
Goal: Return a boolean array where each element indicates whether the corresponding query can form a palindrome.
Example: For substring
You're given a string
s and multiple queries. For each query [left, right, k], you need to determine if the substring s[left...right] can be rearranged and have up to k characters replaced to form a palindrome.Key Operations Allowed:
1. Rearrange characters in the substring in any order
2. Replace up to
k characters with any lowercase letterGoal: Return a boolean array where each element indicates whether the corresponding query can form a palindrome.
Example: For substring
"abcd" with k=2, we can rearrange to "abdc" and replace "bd" with "aa" to get "aaac", then rearrange to "acaa" - but this isn't optimal. Better: rearrange to "abcd", replace "cd" with "ab" to get "abab", then rearrange to "abba" (palindrome!). Input & Output
example_1.py โ Basic Queries
$
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]
๐ก Note:
Query [3,3,0]: substring='d', already palindrome. Query [1,2,0]: substring='bc', 2 odd chars, need 1 replacement but k=0. Query [0,3,1]: substring='abcd', 4 odd chars, need 2 replacements but k=1. Query [0,3,2]: substring='abcd', need 2 replacements and k=2. Query [0,4,1]: substring='abcda', 3 odd chars, need 1 replacement and k=1.
example_2.py โ Edge Cases
$
Input:
s = "lyb", queries = [[0,1,0],[2,2,1]]
โบ
Output:
[false, true]
๐ก Note:
Query [0,1,0]: substring='ly', 2 odd chars, need 1 replacement but k=0. Query [2,2,1]: substring='b', already palindrome (single character).
example_3.py โ All Same Characters
$
Input:
s = "aaaa", queries = [[0,3,0],[1,2,1]]
โบ
Output:
[true, true]
๐ก Note:
Query [0,3,0]: substring='aaaa', all same chars (even count), already palindrome. Query [1,2,1]: substring='aa', even count, already palindrome.
Visualization
Tap to expand
Understanding the Visualization
1
Count Character Frequencies
For each character in the substring, count how many times it appears
2
Identify Unpaired Characters
Characters with odd frequencies can't all be paired - some will be left over
3
Calculate Minimum Replacements
Need to replace (oddCount / 2) characters to make pairs for palindrome sides
4
Check Against K
If minimum replacements โค k, we can form a palindrome
Key Takeaway
๐ฏ Key Insight: A palindrome needs at most 1 character with odd frequency. Count odd frequencies and check if (oddCount/2) โค k replacements can fix the imbalance.
Time & Space Complexity
Time Complexity
O(N + Q)
O(N) to build prefix array, O(1) per query to calculate XOR and count bits
โ Linear Growth
Space Complexity
O(N)
Store prefix XOR array of size N+1
โ Linear Space
Constraints
- 1 โค s.length, queries.length โค 105
- 0 โค lefti โค righti < s.length
- 0 โค ki โค s.length
- s consists only of lowercase English letters
- Each query is independent - no modifications persist
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code