Number of Equal Count Substrings
01 · Problem
You are given a string s of lowercase English letters and a positive integer count.
A substring is called an equal count substring if every distinct letter that appears in it appears exactly count times.
Return the number of equal count substrings of s. Substrings are counted by position, so identical text at different positions is counted separately. Return 0 if there are none.
02 · Examples
s = "xxyyxy", count = 2
4
The qualifying substrings are "xx" (indices 0-1), "yy" (indices 2-3), "xxyy" (indices 0-3) and "xyyx" (indices 1-4). In each, every letter that appears does so exactly twice.
s = "abcd", count = 2
0
Every letter appears only once in the whole string, so no substring can contain a letter exactly twice.
s = "a", count = 5
0
The string is shorter than count, so no substring qualifies.
03 · Constraints
- 011 <= s.length <= 3 * 104
- 021 <= count <= 3 * 104
- 03s consists only of lowercase English letters
04 · Optimal complexity
- Time
- O(26 * n)
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.