↓Skip to main content
  1. LeetCode/

LeetCode 3258: Count Substrings That Satisfy K-Constraint I

·2 mins· ·
LeetCode Easy Sliding-Window String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-18

  • Submissions: Accepted (first try)

Approach
#

Valid ⇔ the count of 0 is <= k or the count of 1 is <= k. Conversely, invalid ⇔ both are > k.

Neither count decreases as the window grows, so “invalid” is monotonic:

  • Add on the right; shrink the left while both counts exceed k.
  • There are right - left + 1 valid substrings ending at right (any left end in [left, right]).

My Solution
#

class Solution:
    def countKConstraintSubstrings(self, s: str, k: int) -> int:
        cnt = Counter()
        left = count = 0
        for right, c in enumerate(s):
            cnt[c] += 1
            while cnt["0"] > k and cnt["1"] > k:
                cnt[s[left]] -= 1
                left += 1
            count += right - left + 1
        return count

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Writing the shrink condition with or: one count <= k is enough to be valid, so shrink only when both exceed k.

Interview Explanation
#

A substring is valid when either the zeros or the ones are at most k, so it is invalid only when both exceed k, which is monotonic. I slide a window that shrinks while both exceed k, and add right - left + 1 valid substrings ending at each right end. Time O(n).

Related

LeetCode 3090: Maximum Length Substring With Two Occurrences
·1 min
LeetCode Easy Sliding-Window Hash-Table String
LeetCode 3090 note: a longest-window slide that shrinks the left whenever the newly added character appears more than twice
LeetCode 1763: Longest Nice Substring
·2 mins
LeetCode Easy Sliding-Window Divide-and-Conquer Hash-Table String Bit-Manipulation
LeetCode 1763 note: a character whose other case never appears cannot be in the answer, so split on it and recurse, preferring the left side on ties
LeetCode 2269: Find the K-Beauty of a Number
·2 mins
LeetCode Easy Sliding-Window Math String
LeetCode 2269 note: convert the number to a string and check every length-k substring; 0 cannot be a divisor
LeetCode 1234: Replace the Substring for Balanced String
·2 mins
LeetCode Medium Sliding-Window String
LeetCode 1234 note: after replacing a window, every character outside it must be at most n / 4; a shortest-window slide tracks the counts outside the window
LeetCode 3813: Vowel-Consonant Score
·1 min
LeetCode Easy String Simulation
LeetCode 3813 note: count letters only, split into vowels and consonants; the score is 0 with no consonants, otherwise integer division
LeetCode 2730: Find the Longest Semi-Repetitive Substring
·2 mins
LeetCode Medium Sliding-Window String
LeetCode 2730 note: the window holds at most one adjacent equal pair; when a new pair arrives, shrink the left until the previous pair leaves