Longest Substring with At Least K Repeating Characters

Given a string `s` and an integer `k`, return the length of the longest substring of `s` such that the frequency of each character in this substring is greater than or equal to `k`. If no such substring exists, return 0.

30:00

Longest Substring with At Least K Repeating Characters
medium
Free written solution
Topics
Companies

Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.

If no such substring exists, return 0.

Example 1:
Input: {"s":"aaabb","k":3}
Output: 3
Constraints:
  • 1≤s.length≤1041 \leq \text{s.length} \leq 10^4

  • s consists of only lowercase English letters.

  • 1≤k≤1051 \leq k \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"aaabb","k":3}

A window cannot just grow or shrink on its own here, so fix how many distinct letters it may hold (target = 1..2) and slide once per target. A window is valid when every letter in it appears at least k = 3 times.

distinct letters allowed

1
2
a
a
a
b
b

count ≥ k

count < k

best window

letter counts in the window (dashed line = k = 3)

empty window

unique: 0
at least k: 0
best: 0
Variables
VariableValue
k3
n5
best0
DepthFunction Call
Stack empty
0/13