- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 82 lines of Java from the credited upstream file 2983.java.
- The implementation visibly relies on sequence storage.
- 4 loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public boolean[] canMakePalindromeQueries(String s, int[][] queries) {3 final int n = s.length();4 5 6 final int[] mirroredDiffs = getMirroredDiffs(s);7 8 final int[][] counts = getCounts(s);9 boolean[] ans = new boolean[queries.length];10 11 for (int i = 0; i < queries.length; i++) {12 13 14 15 int[] query = queries[i];16 final int a = query[0];17 final int b = query[1] + 1;18 final int c = query[2];19 final int d = query[3] + 1;20 final int ra = n - a; 21 final int rb = n - b; 22 final int rc = n - c; 23 final int rd = n - d; 24 25 if ((Math.min(a, rd) > 0 && mirroredDiffs[Math.min(a, rd)] > 0) ||26 (n / 2 > Math.max(b, rc) && mirroredDiffs[n / 2] - mirroredDiffs[Math.max(b, rc)] > 0) ||27 (rd > b && mirroredDiffs[rd] - mirroredDiffs[b] > 0) ||28 (a > rc && mirroredDiffs[a] - mirroredDiffs[rc] > 0)) {29 ans[i] = false;30 } else {31 32 33 34 int[] leftRangeCount = subtractArrays(counts[b], counts[a]);35 int[] rightRangeCount = subtractArrays(counts[d], counts[c]);36 if (a > rd)37 rightRangeCount =38 subtractArrays(rightRangeCount, subtractArrays(counts[Math.min(a, rc)], counts[rd]));39 if (rc > b)40 rightRangeCount =41 subtractArrays(rightRangeCount, subtractArrays(counts[rc], counts[Math.max(b, rd)]));42 if (c > rb)43 leftRangeCount =44 subtractArrays(leftRangeCount, subtractArrays(counts[Math.min(c, ra)], counts[rb]));45 if (ra > d)46 leftRangeCount =47 subtractArrays(leftRangeCount, subtractArrays(counts[ra], counts[Math.max(d, rb)]));48 ans[i] = Arrays.stream(leftRangeCount).allMatch(freq -> freq >= 0) &&49 Arrays.stream(rightRangeCount).allMatch(freq -> freq >= 0) &&50 Arrays.equals(leftRangeCount, rightRangeCount);51 }52 }53 54 return ans;55 }56 57 private int[] getMirroredDiffs(final String s) {58 int[] diffs = new int[s.length() / 2 + 1];59 for (int i = 0, j = s.length() - 1; i < j; i++, j--) {60 diffs[i + 1] = diffs[i] + (s.charAt(i) != s.charAt(j) ? 1 : 0);61 }62 return diffs;63 }64 65 private int[][] getCounts(final String s) {66 int[][] counts = new int[s.length() + 1][26];67 int[] count = new int[26];68 for (int i = 0; i < s.length(); ++i) {69 ++count[s.charAt(i) - 'a'];70 System.arraycopy(count, 0, counts[i + 1], 0, 26);71 }72 return counts;73 }74 75 private int[] subtractArrays(int[] a, int[] b) {76 int[] res = new int[a.length];77 for (int i = 0; i < a.length; ++i)78 res[i] = a[i] - b[i];79 return res;80 }81}82