Skip to content

Commit 522ff5e

Browse files
authored
Create 1177-can-make-palindrome-from-substring.js
1 parent 4c50285 commit 522ff5e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @param {number[][]} queries
4+
* @return {boolean[]}
5+
*/
6+
const canMakePaliQueries = function(s, queries) {
7+
const code = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0)
8+
const preCount = [...s].reduce(
9+
(a, c) => {
10+
let nc = a[a.length - 1]
11+
nc ^= 1 << code(c) //NOT on one bit
12+
a.push(nc)
13+
return a
14+
},
15+
[0]
16+
)
17+
return queries.map(q => {
18+
let subCount = preCount[q[1] + 1] ^ preCount[q[0]]
19+
let oddChs = 0
20+
while (subCount > 0) {
21+
oddChs += subCount & 1
22+
subCount >>= 1
23+
}
24+
return Math.floor(oddChs / 2) <= q[2]
25+
})
26+
}

0 commit comments

Comments
 (0)