↓Skip to main content
  1. LeetCode/

LeetCode 3325: Count Substrings With K-Frequency Characters I

·2 mins· ·
LeetCode Medium Sliding-Window Hash-Table 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: medium First Attempt: 2026-01-19

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer ×2 → Accepted

Approach
#

“At least one character appears at least k times” is monotonic, so use the “at least” count:

  • Add character c on the right; if c just reached k, the window is valid.
  • While valid, keep shrinking the left until some character’s count falls from k to k - 1 (the only qualifying character just left).
  • count += left.

Before adding c the window was invalid (no character at k), and only c can qualify after adding it, so valid ⇔ cnt[c] >= k, and shrinking until cnt[c] < k is enough.

My Attempts
#

VersionProblem
Attempt 1, WAHard-coded k as 2. For s = "b", k = 1, the answer is 1; it returned 0
Attempt 2, REUsed k, but the “no longer valid” check on removal was count == 1 instead of == k - 1. With k = 1 the count drops to 0, never equals 1, and left ran off the string
Attempts 3–4, WAGuarded with left < n and left <= right to stop the overflow, but the threshold was still wrong. For s = "cc", k = 1, the answer is 3 and attempt 3 returned 4; for s = "bbb", k = 3, the answer is 1 and attempt 4 returned 2
ACChanged the threshold to == k - 1

Solution
#

class Solution:
    def numberOfSubstrings(self, s: str, k: int) -> int:
        cnt = Counter()
        left = count = 0
        for c in s:
            cnt[c] += 1
            while cnt[c] >= k:                     # only the new c can qualify
                cnt[s[left]] -= 1
                left += 1
            count += left
        return count

Using cnt[c] >= k directly is simpler than tracking a qualifying count and checking == k - 1, and leaves no threshold to get wrong.

Complexity
#

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

Mistakes to Avoid
#

  • Hard-coding k.
  • The “leaves the target” threshold is k - 1, not 1.

Interview Explanation
#

This is an “at least” count. Before adding a character the window is invalid, and afterwards only that character can reach k, so when it does I keep shrinking the left until it is below k; every left end from 0 to left - 1 is then valid, so I add left. Time O(n).

Related

LeetCode 1358: Number of Substrings Containing All Three Characters
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 1358 note: an “at least one each of a, b, c” counting window: keep shrinking while all three are present, then count += left
LeetCode 395: Longest Substring with At Least K Repeating Characters
·3 mins
LeetCode Medium Sliding-Window Divide-and-Conquer Hash-Table String
LeetCode 395 note: “every character at least k times” is not monotonic, so a direct slide fails; divide and conquer splits on characters with fewer than k occurrences, or fixing the number of distinct characters makes the window slidable
LeetCode 2516: Take K of Each Character From Left and Right
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 2516 note: taking from both ends leaves a middle block in which each letter may appear at most total − k times; find the longest such window
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once