FastPrepLongest Substring with At Least K Repeating Characters

Longest Substring with At Least K Repeating Characters

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given a string s and an integer k, return the length of the longest substring in which every distinct character appears at least k times.

If no nonempty substring satisfies the condition, return 0.

Function

longestSubstring(s: String, k: int) → int

Examples

Example 1

s = "aaabb"k = 3return = 3

The substring aaa contains one distinct character exactly three times.

Example 2

s = "ababbc"k = 2return = 5

In ababb, a occurs twice and b occurs three times.

Example 3

s = "abc"k = 2return = 0

No character repeats twice in any substring.

Constraints

  • 0 <= s.length <= 100000.
  • s contains only lowercase English letters.
  • 1 <= k <= 100000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int longestSubstring(String s, int k) {
    // Write your code here.
}
s"aaabb"
k3
expected3
Checking account…