Longest Substring with At Least K Repeating Characters
Real-world version: Longest Solid Stretch Of The Intake LogThe statement here is our own: the same inputs, outputs and technique.
An intake log is the lowercase string s, one letter per event, listed in the order the events arrived.
Take any stretch of consecutive events. Call the stretch solid when every letter that occurs anywhere inside it occurs at least k times inside it. Return the length of the longest solid stretch, or 0 when no non-empty stretch is solid.
Examples
Example 1
- Input
- s = "aaabbbddcaaabbbdd", k = 3
- Output
- 6
The stretch of events 0 through 5 spells aaabbb, where a occurs three times and b occurs three times, so it is solid and its length is 6.
Example 2
- Input
- s = "abcabc", k = 3
- Output
- 0
Each of a, b and c occurs twice in the whole log, so no letter reaches three occurrences anywhere and no non-empty stretch is solid.
Example 3
- Input
- s = "aabb", k = 2
- Output
- 4
The whole log holds a twice and b twice, which meets the requirement of at least two occurrences each, so the answer is its full length 4.
Constraints
- 1 <= s.length <= 10^4
- s consists of only lowercase English letters.
- 1 <= k <= 10^5
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_substring(s: str, k: int) -> int:public int longestSubstring(String s, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Nice SubstringReal-world version: Longest Matched-Case RunEasy
- Maximum Length Substring With Two OccurrencesReal-world version: Longest Twice-Only StretchEasy
- Substrings of Size Three with Distinct CharactersReal-world version: Three Clean Pigment WindowsEasy
- Longest Substring Without Repeating CharactersReal-world version: Largest Clean Tap BlockMedium