Largest Substring Between Two Equal Characters (LeetCode 1624): Solution and Explanation

Easy
Hash table
String

Find the longest stretch of characters that sits strictly between two equal letters of a lowercase string. Return -1 when no letter repeats.

Intuition

Fix one letter, say 'a'. Among all pairs of 'a's, the widest one uses the leftmost 'a' and the rightmost 'a', because moving either end inward only shrinks the gap.

So for each letter only its first index matters. Every later copy is a candidate right end, and the last copy gives the widest gap for that letter.

One left-to-right pass does it: pin the first index of each letter, and whenever a letter repeats at i, the gap is i - first[c] - 1.

Approach: First occurrence table

  1. Create first, mapping each letter to the index where it first appears, and set best = -1.
  2. Walk the string with index i and letter c.
  3. If c is already in first, update best to the larger of best and i - first[c] - 1.
  4. Otherwise store first[c] = i.
  5. Return best.

Step-by-step example

s = "abca"
  1. i = 0, 'a': first time, pin first['a'] = 0.
  2. i = 1, 'b': first time, pin first['b'] = 1.
  3. i = 2, 'c': first time, pin first['c'] = 2.
  4. i = 3, 'a': seen at 0, gap = 3 - 0 - 1 = 2, so best = 2.
  5. Return 2, the substring "bc".

Code: Python, Java, C++

1class Solution:
2    def maxLengthBetweenEqualCharacters(self, s: str) -> int:
3        first = {}
4        best = -1
5        for i, c in enumerate(s):
6            if c in first:
7                best = max(best, i - first[c] - 1)
8            else:
9                first[c] = i
10        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each character is visited once with a constant-time table lookup.

Space

O(1)

The table holds at most 26 entries, one per lowercase letter.

Other approaches

Check every pair (time O(n^2), space O(1))

For each pair i < j with s[i] == s[j], take j - i - 1. Simple but quadratic.

First and last index per letter (time O(n), space O(1))

Record both the first and last index of each letter, then take the largest last - first - 1. Same complexity, two small passes.

Common mistakes

  • Overwriting first[c] on every occurrence. That stores the latest index and shrinks every later gap.
  • Forgetting the -1: the two equal characters themselves are not part of the substring, so "aa" gives 0, not 2.
  • Starting best at 0. A string with no repeated letter must return -1.

Frequently asked questions

Why is "aa" 0 and not -1?

The two 'a's form a valid pair, and the substring between them is empty, which has length 0. -1 is only for strings where no letter repeats.

Why keep the first index and not the last one?

Scanning left to right, the current index is the right end. The gap is largest when the left end is as far left as possible, which is the first occurrence.

Is a 26-element array better than a hash map?

For lowercase letters an array of 26 entries filled with -1 is the simplest and fastest. A hash map works the same way for any alphabet.