Ctrl K

Widest Letter Gap

Hard
You are given a string text of lowercase English letters. The gap of a substring is the largest difference between the counts of two letters that **both occur** in that substring (the same letter may be taken twice, giving 0). Find the largest gap over all non-empty substrings of text (a substring is a contiguous piece of the string).
Example 1
text="bbabbbb" → 5

The whole string has six bs and one a, a gap of 6 − 1 = 5. Leaving out the a gives only bs and a gap of 0.

Example 2
text="ppqpq" → 2

ppqp has three ps and one q, a gap of 2. Nothing does better.

Example 3
text="xyz" → 0

Every letter occurs once, so every gap is 0.

Constraints
  • 1 ≤ len(text) ≤ 10⁴
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
text="bbabbbb"
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 27 tests on the server, hidden ones included.