Skip to content

Practice Count Substrings That Satisfy K-Constraint II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3261
LeetCode Problem

Count Substrings That Satisfy K-Constraint II

Problem Statement

Given a binary string s, k, and inclusive queries [l, r], count the nonempty substrings contained in s[l..r] having at most k zeroes or at most k ones. Satisfying either condition is sufficient. Return one count per query in order.

Source: Count Substrings That Satisfy K-Constraint II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "0001111", k = 2, queries = [[0,6]]
Output
[26]

# Example 2

Input
s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]]
Output
[15,9,3]

Constraints

1 <= s.length <= 10^5
s[i] is either '0' or '1'.
1 <= k <= s.length
1 <= queries.length <= 10^5
queries[i] == [li, ri]
0 <= li <= ri < s.length
All queries are distinct.

Related LeetCode Problems

Practice more problems similar to Count Substrings That Satisfy K-Constraint II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.