Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
Given a string s and an integer k, return the total number of substrings of s where at least one character appears at least k times.
Example 1:
Input: s = "abacb", k = 2
Output: 4
Explanation:
The valid substrings are:
"aba" (character 'a' appears 2 times)."abac" (character 'a' appears 2 times)."abacb" (character 'a' appears 2 times)."bacb" (character 'b' appears 2 times).Example 2:
Input: s = "abcde", k = 1
Output: 15
Explanation:
All substrings are valid because every character appears at least once.
Constraints:
1 <= s.length <= 30001 <= k <= s.lengths consists only of lowercase English letters.Problem summary: Given a string s and an integer k, return the total number of substrings of s where at least one character appears at least k times.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Sliding Window
"abacb" 2
"abcde" 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
class Solution {
public int numberOfSubstrings(String s, int k) {
int[] cnt = new int[26];
int ans = 0, l = 0;
for (int r = 0; r < s.length(); ++r) {
int c = s.charAt(r) - 'a';
++cnt[c];
while (cnt[c] >= k) {
--cnt[s.charAt(l) - 'a'];
l++;
}
ans += l;
}
return ans;
}
}
// Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
func numberOfSubstrings(s string, k int) (ans int) {
l := 0
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
for cnt[c-'a'] >= k {
cnt[s[l]-'a']--
l++
}
ans += l
}
return
}
# Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
class Solution:
def numberOfSubstrings(self, s: str, k: int) -> int:
cnt = Counter()
ans = l = 0
for c in s:
cnt[c] += 1
while cnt[c] >= k:
cnt[s[l]] -= 1
l += 1
ans += l
return ans
// Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
// class Solution {
// public int numberOfSubstrings(String s, int k) {
// int[] cnt = new int[26];
// int ans = 0, l = 0;
// for (int r = 0; r < s.length(); ++r) {
// int c = s.charAt(r) - 'a';
// ++cnt[c];
// while (cnt[c] >= k) {
// --cnt[s.charAt(l) - 'a'];
// l++;
// }
// ans += l;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3325: Count Substrings With K-Frequency Characters I
function numberOfSubstrings(s: string, k: number): number {
let [ans, l] = [0, 0];
const cnt: number[] = Array(26).fill(0);
for (const c of s) {
const x = c.charCodeAt(0) - 'a'.charCodeAt(0);
++cnt[x];
while (cnt[x] >= k) {
--cnt[s[l++].charCodeAt(0) - 'a'.charCodeAt(0)];
}
ans += l;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.