> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Substring Between Two Equal Characters

> Tested Python solution for LeetCode 1624 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1624, [Easy](/catalog/easy). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/largest-substring-between-two-equal-characters/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1624   # by problem number
lcpy gen -s largest_substring_between_two_equal_characters   # by problem name
```

## Problem

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.

### Examples

```
Input: s = "aa"
Output: 0
Explanation: The optimal substring here is an empty substring between the two 'a's.
```

```
Input: s = "abca"
Output: 2
Explanation: The optimal substring here is "bc".
```

```
Input: s = "cbzxy"
Output: -1
Explanation: There are no characters that appear twice in s.
```

### Constraints

* 1 \<= s.length \<= 300
* s contains only lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_substring_between_two_equal_characters/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_substring_between_two_equal_characters/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_length_between_equal_characters(self, s: str) -> int:
        best = -1
        first: dict[str, int] = {}
        for i, ch in enumerate(s):
            j = first.setdefault(ch, i)
            if j != i:
                best = max(best, i - j - 1)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.