> For the complete documentation index, see [llms.txt](https://luj.gitbook.io/code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luj.gitbook.io/code/two-pointers/sliding-windows/longest-substring-with-at-most-k-distinct-characters.md).

# Longest Substring with At Most K Distinct Characters

Given a string*s*, find the length of the longest substring T that contains at most k distinct characters.

## Example

For example, Given s =`"eceba"`,`k = 3`,

T is`"eceb"`which its length is`4`.

## Code

```java
public class Solution {
    /**
     * @param s: A string
     * @param k: An integer
     * @return: An integer
     */
    public int lengthOfLongestSubstringKDistinct(String s, int k) {
        // write your code here
        int res = 0;
        if (s == null || s.length() == 0) {
            return res;
        }
        int len = s.length();
        int j = 0, count = 0;
        int[] set = new int[256];
        for (int i = 0; i < len; i++) {
            while (j < len && count <= k) {
                if (set[s.charAt(j)]++ == 0) {
                    if (count == k) {
                        set[s.charAt(j)]--; //otherwise j will process one more
                        break;
                    }
                    count++;
                }
                j++;
            }

            res = Math.max(res, j - i);

            if (set[s.charAt(i)] == 1) {
                count--;
            }
            set[s.charAt(i)]--;
        }

        return res;
    }
}
```
