Count Substrings That Satisfy K-Constraint I - Problem

You're given a binary string s (containing only '0's and '1's) and an integer k. Your task is to find how many substrings of s satisfy the k-constraint.

A substring satisfies the k-constraint if at least one of these conditions is true:

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

Example: If s = "10101" and k = 1, the substring "101" has 2 zeros and 1 one. Since the number of ones (1) ≀ k (1), it satisfies the constraint!

Return the total count of all valid substrings.

Input & Output

example_1.py β€” Basic Case
$ Input: s = "10101", k = 1
β€Ί Output: 12
πŸ’‘ Note: The valid substrings are: "1" (1 one), "10" (1 zero, 1 one), "101" (1 zero), "0" (1 zero), "01" (1 zero, 1 one), "010" (2 zeros, 1 one - ones≀k), "1" (1 one), "10" (1 zero, 1 one), "101" (1 zero), "0" (1 zero), "01" (1 zero, 1 one), "1" (1 one). Total: 12 substrings.
example_2.py β€” All Valid
$ Input: s = "1001", k = 2
β€Ί Output: 10
πŸ’‘ Note: With k=2, most substrings are valid since we allow up to 2 of each character. All 10 possible substrings satisfy the k-constraint: "1", "10", "100", "1001", "0", "00", "001", "0", "01", "1".
example_3.py β€” Edge Case
$ Input: s = "111", k = 1
β€Ί Output: 6
πŸ’‘ Note: All substrings contain only 1's, so they all satisfy 'ones ≀ k' condition. The 6 substrings are: "1" (Γ—3), "11" (Γ—2), "111" (Γ—1). Total: 3+2+1 = 6.

Constraints

  • 1 ≀ s.length ≀ 105
  • s[i] is either '0' or '1'
  • 0 ≀ k ≀ s.length
  • Each substring must be contiguous

Visualization

Tap to expand
Sliding Window K-Constraint AlgorithmProduction Line: "10101" (k=1)10101Current WindowWindow Analysis:Position 0: Window="1" β†’ zeros=0, ones=1βœ“ ones(1) ≀ k(1) β†’ Valid! Add 1 substringPosition 1: Window="10" β†’ zeros=1, ones=1βœ“ Both counts ≀ k(1) β†’ Valid! Add 2 substringsPosition 2: Window="101" β†’ zeros=1, ones=2βœ“ zeros(1) ≀ k(1) β†’ Valid! Add 3 substringsNote: Even though ones > k, zeros ≀ k makes it valid!🎯 Key InsightThe sliding window efficiently counts ALL valid substrings by:1) Finding the longest valid window for each position, 2) Counting substrings in O(1) time
Understanding the Visualization
1
Set Up Window
Start with an empty window at the beginning of the string
2
Expand Window
Keep adding items to the right side of the window
3
Check Validity
Window is valid if zeros ≀ k OR ones ≀ k (at least one condition)
4
Shrink If Needed
If both zeros > k AND ones > k, remove items from the left
5
Count Substrings
For current right position, count all valid substrings ending here
Key Takeaway
🎯 Key Insight: Instead of checking every substring individually (O(n³)), we use a sliding window to find the furthest valid starting position for each ending position, then count all valid substrings in O(1) time per position, achieving optimal O(n) complexity.
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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