Number of Equal Count Substrings
The problem
You are given a 0-indexed string s consisting of only lowercase English letters, and an integer count. A substring of s is said to be an equal count substring if, for each unique letter in the substring, it appears exactly count times in the substring.
Return the number of equal count substrings in s.
A substring is a contiguous non-empty sequence of characters within a string.
Input: s = "aaabcbbcc", count = 3 Output: 3 Explanation: The substring that starts at index 0 and ends at index 2 is "aaa". The letter 'a' in the substring appears exactly 3 times. The substring that starts at index 3 and ends at index 8 is "bcbbcc". The letters 'b' and 'c' in the substring appear exactly 3 times. The substring that starts at index 0 and ends at index 8 is "aaabcbbcc". The letters 'a', 'b', and 'c' in the substring appear exactly 3 times.
Input: s = "abcd", count = 2 Output: 0 Explanation: The number of times each letter appears in s is less than count. Therefore, no substrings in s are equal count substrings, so return 0.
Consider the string s = "aabbbcccc" and count = 2. How many equal count substrings exist?
- 1 <= s.length <= 3 * 104
- 1 <= count <= 3 * 104
- s consists only of lowercase English letters.
cpp
class Solution {
public:
int equalCountSubstrings(string s, int count) {
// Your code goes here
}
};java
class Solution {
public int equalCountSubstrings(String s, int count) {
// Your code goes here
}
}python
class Solution:
def equalCountSubstrings(self, s: str, count: int) -> int:
# Your code goes herejavascript
class Solution {
equalCountSubstrings(s, count) {
// Your code goes here
}
}csharp
public class Solution
{
public int EqualCountSubstrings(string s, int count)
{
// Your code goes here
}
}go
func equalCountSubstrings(s string, count int) int {
// Your code goes here
}Stuck? Show a way to structure it+
- 01Enumerate possible distinct-letter counts from one to 26.
- 02Set window length to distinctCount times count.
- 03Slide a fixed-size window.
- 04Track how many characters occur exactly count times.
- 05Count windows where all present characters qualify.
Reference answer
Then expect these follow-ups
How does a large arbitrary alphabet change this?
Tests: constraints
Can you return the matching windows?
Tests: index tracking
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question