Number of Equal Count Substrings - 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 & Output

Example 1 — Basic Case
$ Input: s = "aaabbbccc", count = 3
Output: 3
💡 Note: The equal count substrings are "aaa", "bbb", and "ccc". Each contains one unique character that appears exactly 3 times.
Example 2 — Mixed Characters
$ Input: s = "abababab", count = 2
Output: 2
💡 Note: The equal count substrings are "abab" (positions 0-3) and "abab" (positions 2-5). Each contains 'a' twice and 'b' twice.
Example 3 — No Valid Substrings
$ Input: s = "abcdef", count = 2
Output: 0
💡 Note: No substring has any character appearing exactly 2 times, so there are 0 equal count substrings.

Constraints

  • 1 ≤ s.length ≤ 1000
  • 1 ≤ count ≤ s.length
  • s consists of only lowercase English letters

Visualization

Tap to expand
Number of Equal Count Substrings (s="aaabbb", count=3)aaabbbInput: s = "aaabbb", count = 3"aaa" ✓"bbb" ✓Process: Check each substring"aaa" - a appears 3 times ✓"bbb" - b appears 3 times ✓Other substrings don't match criteriaResult: 2
Understanding the Visualization
1
Input
String s and target count value
2
Process
Check each substring's character frequencies
3
Output
Count of valid equal count substrings
Key Takeaway
🎯 Key Insight: A substring is valid if every unique character in it appears exactly count times
Asked in
Google 25 Microsoft 20 Amazon 15 Facebook 12
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen