Problem solution · Java

Count Substrings That Satisfy K-Constraint II

Count Substrings That Satisfy K-Constraint II: a Java solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Count Substrings That Satisfy K-Constraint II, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 58 lines of Java from the credited upstream file 3261.java.
  • The implementation visibly relies on sequence storage.
  • 6 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCount Substrings That Satisfy K-Constraint II · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public long[] countKConstraintSubstrings(String s, int k, int[][] queries) {    final int n = s.length();    long[] ans = new long[queries.length];    int[] count = new int[2];    // leftToRight[l] : = the maximum right index r s.t.s[l..r] is valid    int[] leftToRight = new int[n];    // rightToLeft[r] := the minimum left index l s.t. s[l..r] is valid    int[] rightToLeft = new int[n];    // prefix[i] := the number of valid substrings ending in [0..i - 1].    long[] prefix = new long[n + 1];     for (int l = 0, r = 0; r < n; ++r) {      ++count[s.charAt(r) - '0'];      while (count[0] > k && count[1] > k)        --count[s.charAt(l++) - '0'];      rightToLeft[r] = l;    }     Arrays.fill(count, 0);     for (int l = n - 1, r = n - 1; l >= 0; --l) {      ++count[s.charAt(l) - '0'];      while (count[0] > k && count[1] > k)        --count[s.charAt(r--) - '0'];      leftToRight[l] = r;    }     for (int r = 0; r < n; ++r)      prefix[r + 1] = prefix[r] + r - rightToLeft[r] + 1;     for (int i = 0; i < queries.length; ++i) {      final int l = queries[i][0];      final int r = queries[i][1];      long numValidSubstrings = 0;      if (r > leftToRight[l]) {        // If r is beyond leftToRight[l], compute the number of valid substrings        // from l to leftToRight[l] and add the number of valid substrings        // ending in [leftToRight[l] + 1..r].        //        // prefix[r + 1] := the number of valid substrings ending in [0..r].        // prefix[leftToRight[l] + 1] := the number of valid substrings ending        // in [0..leftToRight].        // => prefix[r + 1] - prefix[leftToRight[l] + 1] := the number of valid        // substrings ending in [leftToRight[l] + 1..r].        final int sz = leftToRight[l] - l + 1;        numValidSubstrings = (sz * (sz + 1)) / 2 + (prefix[r + 1] - prefix[leftToRight[l] + 1]);      } else {        final int sz = r - l + 1;        numValidSubstrings = (sz * (long) (sz + 1)) / 2;      }      ans[i] = numValidSubstrings;    }     return ans;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗