Skip to content
MediumSliding WindowAI interview only

Number of Equal Count Substrings

Asked atgoogleamazonmeta

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

Example 01
Input
s = "xxyyxy", count = 2
Output
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.

Example 02
Input
s = "abcd", count = 2
Output
0

Every letter appears only once in the whole string, so no substring can contain a letter exactly twice.

Example 03
Input
s = "a", count = 5
Output
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)
05 · Two ways to work on it

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.