Count Substrings With K-Frequency Characters II - Problem
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.
A substring is a contiguous sequence of characters within a string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abcb", k = 2
›
Output:
2
💡 Note:
Substrings with at least one character appearing ≥ 2 times: "bcb" (b appears 2 times) and "abcb" (b appears 2 times). Total count is 2.
Example 2 — No Valid Substrings
$
Input:
s = "abc", k = 2
›
Output:
0
💡 Note:
No character appears 2 or more times in any substring, so result is 0.
Example 3 — All Same Characters
$
Input:
s = "aaaa", k = 2
›
Output:
6
💡 Note:
All substrings of length ≥ 2 are valid since 'a' appears at least 2 times: "aa" (3 occurrences), "aaa" (2 occurrences), "aaaa" (1 occurrence). Total = 6.
Constraints
- 1 ≤ s.length ≤ 104
- 1 ≤ k ≤ s.length
- s consists of only lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code