Domain 1EasyCORE

Largest Substring Between Two Equal Characters

The problem requires finding the length of the longest substring between two equal characters in a given string `s`. The substring should exclude the two equal characters themselves. If no such substring exists, return `-1`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

1624. Largest Substring Between Two Equal Characters

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 58.6%


Problem Breakdown

The problem requires finding the length of the longest substring between two equal characters in a given string s. The substring should exclude the two equal characters themselves. If no such substring exists, return -1.

Inputs:

  • A single string s containing only lowercase English letters.

Outputs:

  • An integer representing the length of the longest substring between two equal characters, or -1 if no such substring exists.

Implicit Constraints and Edge Cases:

  • The string can be as short as 1 character.
  • If all characters are unique, the output should be -1.
  • If there are multiple pairs of equal characters, we need to find the longest one.
  • The string length is limited to 300 characters.

Intuition

A brute-force approach would involve checking every possible pair of equal characters and calculating the substring length between them. This would result in a time complexity of O(n^2), which is inefficient given the constraints.

The key insight is to use a dictionary to store the first occurrence index of each character as we iterate through the string. By doing this, we can efficiently calculate the length of the substring between the first and last occurrence of each character. This reduces the time complexity to O(n).

Approach

  1. Initialize an empty dictionary first_occurrence to store the first occurrence index of each character.
  2. Initialize a variable max_length to -1 to keep track of the maximum length found.
  3. Iterate through the string using an index i and character char.
    • If char is not in first_occurrence, add it with its current index i.
    • If char is already in first_occurrence, calculate the substring length between the first occurrence and the current index i. Update max_length if this length is greater than the current max_length.
  4. Return max_length.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the string. We iterate through the string once.
  • Space Complexity: O(1) for the dictionary, as it can contain at most 26 entries (one for each lowercase English letter).

Solution

def maxLengthBetweenEqualCharacters(s: str) -> int:
    first_occurrence = {}
    max_length = -1
    
    for i, char in enumerate(s):
        if char not in first_occurrence:
            first_occurrence[char] = i
        else:
            length = i - first_occurrence[char] - 1
            max_length = max(max_length, length)
    
    return max_length

Edge Cases Handled

  • Single Character String: If the string has only one character, max_length remains -1, as no substring can be formed.
  • All Unique Characters: The dictionary will store all characters with their first occurrence index, and max_length will remain -1.
  • Multiple Pairs of Equal Characters: The algorithm correctly identifies the longest substring between any pair of equal characters.