|
| 1 | +function canMakePalindromeQueries(s, queries) { |
| 2 | + const n = s.length |
| 3 | + // Prefix sum (difference) |
| 4 | + const psd = [0] |
| 5 | + for (let i = 0, j = n - 1; i < j; i++, j--) { |
| 6 | + psd.push(psd[psd.length - 1] + (s[i] !== s[j] ? 1 : 0)) |
| 7 | + } |
| 8 | + // Prefix sum (count) |
| 9 | + const cnt = new Array(26).fill(0) |
| 10 | + const psc = [cnt.slice()] |
| 11 | + for (const c of s) { |
| 12 | + cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++ |
| 13 | + psc.push([...cnt]) |
| 14 | + } |
| 15 | + const ans = [] |
| 16 | + for (const q of queries) { |
| 17 | + const a1 = q[0], |
| 18 | + b1 = q[1] + 1, |
| 19 | + a2 = n - q[0], |
| 20 | + b2 = n - 1 - q[1] |
| 21 | + const c1 = q[2], |
| 22 | + d1 = q[3] + 1, |
| 23 | + c2 = n - q[2], |
| 24 | + d2 = n - 1 - q[3] |
| 25 | + // No difference allowed outside the query ranges |
| 26 | + if ( |
| 27 | + (min(a1, d2) && psd[min(a1, d2)]) || |
| 28 | + (n / 2 > max(b1, c2) && psd[Math.floor(n / 2)] - psd[max(b1, c2)]) || |
| 29 | + (d2 > b1 && psd[d2] - psd[b1]) || |
| 30 | + (a1 > c2 && psd[a1] - psd[c2]) |
| 31 | + ) { |
| 32 | + ans.push(false) |
| 33 | + } else { |
| 34 | + // Intersection of query ranges in the lower half must equate to that in the upper half |
| 35 | + const ix1 = psc[d1].map((val, i) => val - (psc[c1][i] || 0)) |
| 36 | + const ix2 = psc[b1].map((val, i) => val - (psc[a1][i] || 0)) |
| 37 | + if (a1 > d2) { |
| 38 | + ix1.forEach( |
| 39 | + (val, i) => (ix1[i] -= psc[Math.min(a1, c2)][i] - (psc[d2][i] || 0)), |
| 40 | + ) |
| 41 | + } |
| 42 | + if (c2 > b1) { |
| 43 | + ix1.forEach( |
| 44 | + (val, i) => (ix1[i] -= psc[c2][i] - (psc[Math.max(b1, d2)][i] || 0)), |
| 45 | + ) |
| 46 | + } |
| 47 | + if (c1 > b2) { |
| 48 | + ix2.forEach( |
| 49 | + (val, i) => (ix2[i] -= psc[Math.min(c1, a2)][i] - (psc[b2][i] || 0)), |
| 50 | + ) |
| 51 | + } |
| 52 | + if (a2 > d1) { |
| 53 | + ix2.forEach( |
| 54 | + (val, i) => (ix2[i] -= psc[a2][i] - (psc[Math.max(d1, b2)][i] || 0)), |
| 55 | + ) |
| 56 | + } |
| 57 | + ans.push(ix1.every((val, i) => val >= 0 && val === ix2[i])) |
| 58 | + } |
| 59 | + } |
| 60 | + return ans |
| 61 | +} |
| 62 | + |
| 63 | +// Helper functions |
| 64 | +function min(a, b) { |
| 65 | + return a < b ? a : b |
| 66 | +} |
| 67 | + |
| 68 | +function max(a, b) { |
| 69 | + return a > b ? a : b |
| 70 | +} |
0 commit comments