-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1177-can-make-palindrome-from-substring.js
48 lines (43 loc) · 1.57 KB
/
1177-can-make-palindrome-from-substring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 1177. Can Make Palindrome from Substring
* https://leetcode.com/problems/can-make-palindrome-from-substring/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {string} s
* @param {number[][]} queries
* @return {boolean[]}
*/
var canMakePaliQueries = function(s, queries) {
const prefixCounts = new Array(s.length + 1).fill().map(() => new Array(26).fill(0));
for (let i = 0; i < s.length; i++) {
prefixCounts[i + 1] = [...prefixCounts[i]];
prefixCounts[i + 1][s.charCodeAt(i) - 97]++;
}
const getOddCount = (left, right) => {
let odds = 0;
for (let j = 0; j < 26; j++) {
const count = prefixCounts[right + 1][j] - prefixCounts[left][j];
odds += count % 2;
}
return odds;
};
const result = queries.map(([left, right, k]) => {
const oddCount = getOddCount(left, right);
return oddCount <= 2 * k + 1;
});
return result;
};