Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a string s and array queries where queries[i] = [lefti, righti, ki]. We may rearrange the substring s[lefti...righti] for each query and then choose up to ki 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 ith query queries[i].
Note that each letter is counted individually for replacement, so if, for example s[lefti...righti] = "aaa", and ki = 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 <= 1050 <= lefti <= righti < s.length0 <= ki <= s.lengths consists of lowercase English letters.Problem summary: You are given a string s and array queries where queries[i] = [lefti, righti, ki]. We may rearrange the substring s[lefti...righti] for each query and then choose up to ki 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 ith query queries[i]. Note that each letter is counted individually for replacement, so if, for example s[lefti...righti] = "aaa", and ki = 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
"abcda" [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
"lyb" [[0,1,0],[2,2,1]]
plates-between-candles)maximize-the-number-of-partitions-after-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1177: Can Make Palindrome from Substring
class Solution {
public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
int n = s.length();
int[][] ss = new int[n + 1][26];
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 26; ++j) {
ss[i][j] = ss[i - 1][j];
}
ss[i][s.charAt(i - 1) - 'a']++;
}
List<Boolean> ans = new ArrayList<>();
for (var q : queries) {
int l = q[0], r = q[1], k = q[2];
int x = 0;
for (int j = 0; j < 26; ++j) {
x += (ss[r + 1][j] - ss[l][j]) & 1;
}
ans.add(x / 2 <= k);
}
return ans;
}
}
// Accepted solution for LeetCode #1177: Can Make Palindrome from Substring
func canMakePaliQueries(s string, queries [][]int) (ans []bool) {
n := len(s)
ss := make([][26]int, n+1)
for i := 1; i <= n; i++ {
for j := 0; j < 26; j++ {
ss[i][j] = ss[i-1][j]
}
ss[i][s[i-1]-'a']++
}
for _, q := range queries {
l, r, k := q[0], q[1], q[2]
x := 0
for j := 0; j < 26; j++ {
x += (ss[r+1][j] - ss[l][j]) & 1
}
ans = append(ans, x/2 <= k)
}
return
}
# Accepted solution for LeetCode #1177: Can Make Palindrome from Substring
class Solution:
def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
n = len(s)
ss = [[0] * 26 for _ in range(n + 1)]
for i, c in enumerate(s, 1):
ss[i] = ss[i - 1][:]
ss[i][ord(c) - ord("a")] += 1
ans = []
for l, r, k in queries:
cnt = sum((ss[r + 1][j] - ss[l][j]) & 1 for j in range(26))
ans.append(cnt // 2 <= k)
return ans
// Accepted solution for LeetCode #1177: Can Make Palindrome from Substring
struct Solution;
impl Solution {
fn can_make_pali_queries(s: String, queries: Vec<Vec<i32>>) -> Vec<bool> {
let n = s.len();
let mut prefix: Vec<u32> = vec![0; n + 1];
for (i, c) in s.char_indices() {
prefix[i + 1] = prefix[i] ^ (1 << (c as u32 - 'a' as u32));
}
let mut res = vec![];
for q in queries {
let left = q[0] as usize;
let right = q[1] as usize + 1;
let k = q[2] as u32;
res.push(k * 2 >= (prefix[right] ^ prefix[left]).count_ones() - 1);
}
res
}
}
#[test]
fn test() {
let s = "abcda".to_string();
let queries = vec_vec_i32![[3, 3, 0], [1, 2, 0], [0, 3, 1], [0, 3, 2], [0, 4, 1]];
let res = vec![true, false, false, true, true];
assert_eq!(Solution::can_make_pali_queries(s, queries), res);
}
// Accepted solution for LeetCode #1177: Can Make Palindrome from Substring
function canMakePaliQueries(s: string, queries: number[][]): boolean[] {
const n = s.length;
const ss: number[][] = Array(n + 1)
.fill(0)
.map(() => Array(26).fill(0));
for (let i = 1; i <= n; ++i) {
ss[i] = ss[i - 1].slice();
++ss[i][s.charCodeAt(i - 1) - 97];
}
const ans: boolean[] = [];
for (const [l, r, k] of queries) {
let x = 0;
for (let j = 0; j < 26; ++j) {
x += (ss[r + 1][j] - ss[l][j]) & 1;
}
ans.push(x >> 1 <= k);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.