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 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 letter

Goal: 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
Palindrome Formation VisualizationExample: "abcd" โ†’ Can we make palindrome with k=2?acount: 1bcount: 1ccount: 1dcount: 1All 4 characters have odd counts โ†’ Need to pair themReplace 2 charsForm pairsStrategy: Replace c,d with a,b โ†’ "abab"Rearrange: "abab" โ†’ "abba" (palindrome!)abbaResult: Need 2 replacements, k=2 โœ“ โ†’ TrueMirror!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(N)

Store prefix XOR array of size N+1

n
2n
โœ“ 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
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
28.4K Views
Medium Frequency
~25 min Avg. Time
842 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