Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "aa"
Output: 0
Explanation: The optimal substring here is an empty substring between the two 'a's.
Example 2:
Input: s = "abca" Output: 2 Explanation: The optimal substring here is "bc".
Example 3:
Input: s = "cbzxy" Output: -1 Explanation: There are no characters that appear twice in s.
Constraints:
1 <= s.length <= 300s contains only lowercase English letters.Problem summary: Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1. A substring is a contiguous sequence of characters within a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"aa"
"abca"
"cbzxy"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1624: Largest Substring Between Two Equal Characters
class Solution {
public int maxLengthBetweenEqualCharacters(String s) {
int[] d = new int[26];
Arrays.fill(d, -1);
int ans = -1;
for (int i = 0; i < s.length(); ++i) {
int j = s.charAt(i) - 'a';
if (d[j] == -1) {
d[j] = i;
} else {
ans = Math.max(ans, i - d[j] - 1);
}
}
return ans;
}
}
// Accepted solution for LeetCode #1624: Largest Substring Between Two Equal Characters
func maxLengthBetweenEqualCharacters(s string) int {
d := make([]int, 26)
for i := range d {
d[i] = -1
}
ans := -1
for i, c := range s {
c -= 'a'
if d[c] == -1 {
d[c] = i
} else {
ans = max(ans, i-d[c]-1)
}
}
return ans
}
# Accepted solution for LeetCode #1624: Largest Substring Between Two Equal Characters
class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
d = {}
ans = -1
for i, c in enumerate(s):
if c in d:
ans = max(ans, i - d[c] - 1)
else:
d[c] = i
return ans
// Accepted solution for LeetCode #1624: Largest Substring Between Two Equal Characters
impl Solution {
pub fn max_length_between_equal_characters(s: String) -> i32 {
let s = s.as_bytes();
let n = s.len();
let mut pos = [-1; 26];
let mut res = -1;
for i in 0..n {
let j = (s[i] - b'a') as usize;
let i = i as i32;
if pos[j] == -1 {
pos[j] = i;
} else {
res = res.max(i - pos[j] - 1);
}
}
res
}
}
// Accepted solution for LeetCode #1624: Largest Substring Between Two Equal Characters
function maxLengthBetweenEqualCharacters(s: string): number {
const n = s.length;
const pos = new Array(26).fill(-1);
let res = -1;
for (let i = 0; i < n; i++) {
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
if (pos[j] === -1) {
pos[j] = i;
} else {
res = Math.max(res, i - pos[j] - 1);
}
}
return res;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.