> ## 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 with At Most Two Distinct

> Tested Python solution for LeetCode 159 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 159, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/description/).

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

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

## Problem

Given a string `s`, return the length of the longest substring that contains at most two distinct characters.

### Examples

```
Input: s = "eceba"
Output: 3
Explanation: The substring is "ece" which its length is 3.
```

```
Input: s = "ccaabbb"
Output: 5
Explanation: The substring is "aabbb" which its length is 5.
```

### Constraints

* 1 \<= s.length \<= 10^5
* s consists of English letters

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def length_of_longest_substring_two_distinct(self, s: str) -> int:
        count: dict[str, int] = {}
        left = 0
        longest = 0
        for right, ch in enumerate(s):
            count[ch] = count.get(ch, 0) + 1
            while len(count) > 2:
                left_ch = s[left]
                count[left_ch] -= 1
                if count[left_ch] == 0:
                    del count[left_ch]
                left += 1
            longest = max(longest, right - left + 1)
        return longest
```

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