Longest Substring in Which Every Present Letter Occurs Equally Often
Company: DRW
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: Given a string of lowercase letters, find the length of the longest substring in which every letter that appears occurs the same number of times. It tests substring enumeration with incremental frequency tracking and a precise definition of what counts as balanced.
Read the full DRW Software Engineer interview experience this question came from
Constraints
- 1 <= len(s) <= 1000
- s consists only of lowercase English letters 'a' to 'z'.
Examples
Input: ('a',)
Expected Output: 1
Explanation: Minimum valid input: a single character is balanced.
Input: ('ab',)
Expected Output: 2
Explanation: Two distinct letters once each: the whole string is balanced.
Hints
- Only letters that actually occur in a substring are compared, so a run of one repeated letter is always balanced and every single character gives an answer of at least 1.
- Balance is not monotonic: "aab" is unbalanced but extending it to "aabb" makes it balanced again, so an unbalanced substring can still grow into a balanced one.
- The string has at most 1000 characters, so examining many substrings is affordable as long as you do not recount each one from scratch.