> ## 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.

# Longest Substring Without Repeating Characters

> Tested Python solution for LeetCode 3 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 3, [Medium](https://leetcode-py.wisl.dev/catalog/medium.md). Topics: [Hash Table](https://leetcode-py.wisl.dev/catalog/topics/hash-table), [String](https://leetcode-py.wisl.dev/catalog/topics/string), [Sliding Window](https://leetcode-py.wisl.dev/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](https://leetcode-py.wisl.dev/practice/testing.md), and a playground notebook:

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

## Problem

Given a string `s`, find the length of the **longest** **substring** without duplicate characters.

### Examples

```
Input: s = "abcabcbb"
Output: 3
```

**Explanation:** The answer is "abc", with the length of 3.

```
Input: s = "bbbbb"
Output: 1
```

**Explanation:** The answer is "b", with the length of 1.

```
Input: s = "pwwkew"
Output: 3
```

**Explanation:** The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

### Constraints

* 0 \<= s.length \<= 5 \* 10^4
* s consists of English letters, digits, symbols and spaces.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(min(m, n)) where m is charset size
    def length_of_longest_substring(self, s: str) -> int:
        seen: set[str] = set()
        left = max_len = 0

        for right in range(len(s)):
            while s[right] in seen:
                seen.remove(s[left])
                left += 1
            seen.add(s[right])
            max_len = max(max_len, right - left + 1)

        return max_len
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(min(m, n)) where m is charset size |

## Tags

[Grind 75](https://leetcode-py.wisl.dev/catalog/grind-75.md), [Grind](https://leetcode-py.wisl.dev/catalog/grind.md), [Blind 75](https://leetcode-py.wisl.dev/catalog/blind-75.md), [NeetCode 150](https://leetcode-py.wisl.dev/catalog/neetcode-150.md), [NeetCode 250](https://leetcode-py.wisl.dev/catalog/neetcode-250.md), [NeetCode All](https://leetcode-py.wisl.dev/catalog/neetcode.md), [AlgoMaster 75](https://leetcode-py.wisl.dev/catalog/algo-master-75.md).


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