> ## 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. Topics: Hash Table, String, 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](/practice/testing), 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](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
