Talentd
FleetCode

2067. Number of Equal Count Substrings

Medium45.1% acceptance1 companyPremium elsewhere, free here

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.

Example 1:

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.

Example 2:

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.

Example 3:

Input: s = "a", count = 5
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

Constraints:

  • 1 <= s.length <= 3 * 104
  • 1 <= count <= 3 * 104
  • s consists only of lowercase English letters.

Asked at 1 company

Hints open one at a time

Hint 1
The brute force solution is to check every substring, which would TLE. How can we improve this solution?
Hint 2
In an equal count substring, the first character appears count times, the second character appears count times, and so on.
Hint 3
The length of an equal count substring is the number of unique characters multiplied by count.
Hint 4
The length of all equal count substrings are multiples of count.
10:56
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"aaabcbbcc"
3
Input
"abcd"
2
Input
"a"
5
Your code is saved on this device.Solution