Count Substrings That Satisfy K-Constraint II - Problem

You are given a binary string s and an integer k. You are also given a 2D integer array queries, where queries[i] = [li, ri].

A binary string satisfies the k-constraint if either of the following conditions holds:

  • The number of '0's in the string is at most k
  • The number of '1's in the string is at most k

For each query, you need to count how many substrings within the range s[li..ri] satisfy the k-constraint.

Goal: Return an integer array answer, where answer[i] is the number of substrings of s[li..ri] that satisfy the k-constraint.

Example: If s = "0001111" and k = 2, then substrings like "00", "11", "001" all satisfy the constraint because they have โ‰ค2 zeros OR โ‰ค2 ones.

Input & Output

example_1.py โ€” Basic Example
$ Input: s = "0001111", k = 2, queries = [[0,6]]
โ€บ Output: [26]
๐Ÿ’ก Note: All substrings of s satisfy the constraint since each has โ‰ค2 zeros OR โ‰ค2 ones. Total substrings = 7ร—8/2 = 28, but some violate the constraint. After checking: 26 valid substrings.
example_2.py โ€” Multiple Queries
$ Input: s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]]
โ€บ Output: [15,9,3]
๐Ÿ’ก Note: Query [0,5]: Full string has many valid substrings. Query [1,4]: Substring "1010" analysis. Query [2,3]: Substring "01" has 3 valid substrings: "0", "1", "01".
example_3.py โ€” Edge Case
$ Input: s = "11111", k = 1, queries = [[0,4]]
โ€บ Output: [15]
๐Ÿ’ก Note: All substrings satisfy constraint (โ‰ค1 ones OR โ‰ค1 zeros). Since all are 1s, each substring has โ‰ค1 zeros, so all 15 possible substrings are valid.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s[i] is either '0' or '1'
  • 1 โ‰ค k โ‰ค s.length
  • 1 โ‰ค queries.length โ‰ค 105
  • queries[i] = [li, ri]
  • 0 โ‰ค li โ‰ค ri < s.length

Visualization

Tap to expand
Production Line (Binary String)0001111Valid SegmentInspector Tool(Sliding Window)Tolerance: k=2 defects of same typeSegment "000": 3 zeros > k=2 โŒSegment "00": 2 zeros โ‰ค k=2 โœ…Manager Query: "How many valid segments in section [1,5]?"Inspector: *checks precomputed data* "Answer: 9 segments"Precomputed Boundary Table:Position 0 โ†’ Valid until 2Position 1 โ†’ Valid until 4Position 2 โ†’ Valid until 6Position 3 โ†’ Valid until 6...
Understanding the Visualization
1
Set Tolerance Level
Establish k as maximum acceptable defects of either type
2
Find Valid Segments
Use sliding window to efficiently find all valid chip segments
3
Precompute Boundaries
Record the furthest valid position for each starting point
4
Answer Inspections
When managers ask about specific sections, use precomputed data
Key Takeaway
๐ŸŽฏ Key Insight: By precomputing the furthest valid position for each starting point using sliding window, we can answer any range query in O(1) time using mathematical formulas, achieving optimal O(N + Q) complexity.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen