Can Make Palindrome from Substring - Problem

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: Each letter is counted individually for replacement, so if s[left_i...right_i] = "aaa" and k_i = 2, we can only replace two of the letters. Also, no query modifies the initial string s.

Input & Output

Example 1 — Multiple Query Types
$ 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" is already palindrome. Query [1,2,0]: "bc" needs 1 replacement, but k=0. Query [0,3,1]: "abcd" has 4 odd chars, needs 1.5≈2 replacements > k=1. Query [0,3,2]: "abcd" needs 2 replacements ≤ k=2. Query [0,4,1]: "abcda" has 2 odd chars (b,c), needs 0.5≈1 replacement ≤ k=1.
Example 2 — Edge Cases
$ Input: s = "lyb", queries = [[0,1,0],[2,2,1]]
Output: [false,true]
💡 Note: Query [0,1,0]: "ly" has 2 odd characters, needs 1 replacement but k=0. Query [2,2,1]: "b" is single character, always palindrome.
Example 3 — All Same Characters
$ Input: s = "aaaa", queries = [[0,3,0],[1,2,1]]
Output: [true,true]
💡 Note: Query [0,3,0]: "aaaa" has all even frequencies, palindrome possible. Query [1,2,1]: "aa" is already palindrome.

Constraints

  • 1 ≤ s.length, queries.length ≤ 105
  • 0 ≤ lefti ≤ righti < s.length
  • 0 ≤ ki ≤ s.length
  • s consists of lowercase English letters

Visualization

Tap to expand
Can Make Palindrome from Substring INPUT String s: a 0 b 1 c 2 d 3 a 4 Queries [left, right, k]: Q0: [3,3,0] - "d" Q1: [1,2,0] - "bc" Q2: [0,3,1] - "abcd" Q3: [0,3,2] - "abcd" Q4: [0,4,1] - "abcda" k = max replacements allowed Can rearrange substring ALGORITHM STEPS 1 Prefix XOR Array Build prefix[i] with bit for each char (a=bit0, b=bit1...) 2 XOR for Range diff = prefix[r+1] XOR prefix[l] Bits show odd-count chars 3 Count Odd Chars oddCount = popcount(diff) Count set bits in XOR result 4 Check Palindrome Need oddCount/2 changes Return (oddCount/2) <= k Example: Q4 [0,4,1] "abcda" has chars: a=2,b=1,c=1,d=1 Odd counts: b,c,d = 3 chars Need 3/2 = 1 replacement k=1 >= 1 --> true "adbca" can be "adbda" (OK) FINAL RESULT Q0: "d" odd=1, 1/2=0 <= 0 true Q1: "bc" odd=2, 2/2=1 > 0 false Q2: "abcd" odd=4, 4/2=2 > 1 false Q3: "abcd" odd=4, 4/2=2 <= 2 true Q4: "abcda" odd=3, 3/2=1 <= 1 true Output Array: [true, false, false, true, true] Key Insight: A string can be rearranged into a palindrome if at most 1 character has an odd count. Using XOR with prefix sums, we can find odd-count chars in O(1) per query. Each replacement can fix 2 odd-count chars, so we need (oddCount / 2) replacements. Time: O(n + q), Space: O(n). TutorialsPoint - Can Make Palindrome from Substring | Optimal Solution (Prefix XOR)
Asked in
Google 25 Facebook 18 Amazon 12
28.5K Views
Medium Frequency
~35 min Avg. Time
892 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