publicclassSolution { public IList<bool> CanMakePaliQueries(string s, int[][] queries) { int n = s.Length; int[] count = newint[n + 1]; for (int i = 0; i < n; i++) { count[i + 1] = count[i] ^ (1 << (s[i] - 'a')); } List<Boolean> res = new List<Boolean>(); for (int i = 0; i < queries.Length; i++) { int l = queries[i][0], r = queries[i][1], k = queries[i][2]; int bits = 0, x = count[r + 1] ^ count[l]; while (x > 0) { x &= x - 1; bits++; } res.Add(bits <= k * 2 + 1); } return res; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defcanMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]: n = len(s) count = [0] * (n + 1) for i inrange(n): count[i + 1] = count[i] ^ (1 << (ord(s[i]) - ord('a'))) res = [] for l, r, k in queries: bits = (count[r + 1] ^ count[l]).bit_count() res.append(bits <= k * 2 + 1) return res
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var canMakePaliQueries = function(s, queries) { const n = s.length; const count = Array(n + 1).fill(0); for (let i = 0; i < n; i++) { count[i + 1] = count[i] ^ (1 << (s[i].charCodeAt(0) - 'a'.charCodeAt(0))); } const res = []; for (const query of queries) { const l = query[0], r = query[1], k = query[2]; let bits = 0, x = count[r + 1] ^ count[l]; while (x > 0) { x &= x - 1; bits++; } res.push(bits <= k * 2 + 1); } return res; }
funccanMakePaliQueries(s string, queries [][]int) []bool { n := len(s) count := make([]int, n + 1) for i := 0; i < n; i++ { count[i + 1] = count[i] ^ (1 << (s[i] - 'a')) } res := make([]bool, len(queries)) for i, query := range queries { l := query[0] r := query[1] k := query[2] bits := 0 x := count[r + 1] ^ count[l] for x > 0 { x &= x - 1 bits++ } res[i] = bits <= k * 2 + 1 } return res }