Count Substrings With K-Frequency Characters I - Problem

You're given a string s and an integer k. Your task is to count how many substrings of s contain at least one character that appears at least k times within that substring.

A substring is a contiguous sequence of characters within a string. For example, in the string "hello", some substrings are "h", "he", "ell", "hello", etc.

Example: If s = "abcab" and k = 2, we need to find substrings where at least one character appears 2 or more times. The substring "abcab" contains 'a' twice and 'b' twice, so it qualifies. The substring "bcab" contains 'b' twice, so it also qualifies.

Can you efficiently count all such valid substrings?

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abcab", k = 2
โ€บ Output: 4
๐Ÿ’ก Note: The valid substrings are: "abca" (a appears 2 times), "bcab" (b appears 2 times), "abcab" (both a and b appear 2 times), "cab" contains "ab" where both appear in the full context. Total count: 4
example_2.py โ€” Single Character
$ Input: s = "aaaa", k = 3
โ€บ Output: 3
๐Ÿ’ก Note: Valid substrings: "aaa" (positions 0-2), "aaa" (positions 1-3), and "aaaa" (positions 0-3). Character 'a' appears at least 3 times in each of these substrings.
example_3.py โ€” No Valid Substrings
$ Input: s = "abcde", k = 2
โ€บ Output: 0
๐Ÿ’ก Note: No character appears more than once in any substring, so no substring satisfies the condition of having at least one character appear k=2 or more times.

Constraints

  • 1 โ‰ค s.length โ‰ค 3000
  • 1 โ‰ค k โ‰ค s.length
  • s consists of only lowercase English letters
  • Time limit: 2 seconds

Visualization

Tap to expand
Character Frequency Detective ๐Ÿ•ต๏ธ๐Ÿ”Evidence: "abcab"abcabInvestigation WindowabcaFrequency Countera: 2 โญ (โ‰ฅ k=2)b: 1c: 1Evidence Found! ๐ŸŽฏSmart Counting LogicOnce 'a' appears 2 times in "abca":โœ“ "abca" is valid (current window)โœ“ "abcab" is valid (extend by 1)Count += (n - j) = (5 - 3) = 2 substringsDetective Process:1Start investigation at position i2Expand window and track character frequencies3Found character with frequency โ‰ฅ k!4Count all valid extensions efficiently5Move to next starting positionWhy This Approach Works๐Ÿ”‘ Key Insight: If a substring has a character with frequency k,then ANY longer substring containing it also satisfies the conditionโšก Optimization: Instead of checking each longer substring individually,count them mathematically: (n - current_position)๐Ÿ“ˆ Result: Reduces time complexity from O(nยณ) to O(nยฒ)
Understanding the Visualization
1
Set up investigation
Position your magnifying glass at the start of the evidence
2
Expand the view
Gradually expand your view to include more characters, keeping track of frequency
3
Found significant evidence
Once you find a character appearing k times, you know this section and all extensions are valid
4
Count efficiently
Instead of checking each extension individually, count them all at once
5
Move to next position
Start a new investigation from the next position
Key Takeaway
๐ŸŽฏ Key Insight: Once any character reaches frequency k in a substring, all longer substrings extending from that point will also be valid, allowing us to count them efficiently rather than checking each one individually.
Asked in
Meta 15 Google 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~15 min Avg. Time
847 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