> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/chapter_2_string/sliding-window-problems/problem-159-longest-substring-with-at-most-two-distinct-characters.md).

# Problem 159: Longest Substring with At Most Two Distinct Characters

> <https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/>

![](/files/-Lpv9yDuiJtyLgAZBfV5)

## 思路

* 和上一题的思路一样，代码略作改动

```java
public class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        int[] map = new int[128];

        int i = 0, j = 0, count = 0, maxLen = 0;
        while (j < s.length()) {
            if (map[s.charAt(j++)]++ == 0) count++;

            while (count > 2) {
                if (map[s.charAt(i++)]-- == 1) count--;
            }
            maxLen = Math.max(maxLen, j - i);
        }

        return maxLen;
    }
}
```
