Largest Substring Between Two Equal Characters (LeetCode 1624): Solution and Explanation
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
- Create
first, mapping each letter to the index where it first appears, and setbest = -1. - Walk the string with index i and letter c.
- If c is already in
first, updatebestto the larger ofbestandi - first[c] - 1. - Otherwise store
first[c] = i. - Return
best.
Step-by-step example
s = "abca"
- i = 0, 'a': first time, pin first['a'] = 0.
- i = 1, 'b': first time, pin first['b'] = 1.
- i = 2, 'c': first time, pin first['c'] = 2.
- i = 3, 'a': seen at 0, gap = 3 - 0 - 1 = 2, so best = 2.
- 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 best1class Solution {
2 public int maxLengthBetweenEqualCharacters(String s) {
3 int[] first = new int[26];
4 Arrays.fill(first, -1);
5 int best = -1;
6 for (int i = 0; i < s.length(); i++) {
7 int c = s.charAt(i) - 'a';
8 if (first[c] >= 0) {
9 best = Math.max(best, i - first[c] - 1);
10 } else {
11 first[c] = i;
12 }
13 }
14 return best;
15 }
16}1class Solution {
2public:
3 int maxLengthBetweenEqualCharacters(string s) {
4 vector<int> first(26, -1);
5 int best = -1;
6 for (int i = 0; i < (int)s.size(); i++) {
7 int c = s[i] - 'a';
8 if (first[c] >= 0) {
9 best = max(best, i - first[c] - 1);
10 } else {
11 first[c] = i;
12 }
13 }
14 return best;
15 }
16};Time and space complexity
O(n)
Each character is visited once with a constant-time table lookup.
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
bestat 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.