public int lengthOfLongestSubstringKDistinct(String s, int k) {
        if (s == null) return 0;

        HashMap<Character, Integer> map = new HashMap<>();
        int l = 0, r = 0;
        int maxLen = 0;
        while (r < s.length()) {
            char chr = s.charAt(r);
            map.put(chr, map.getOrDefault(chr, 0) + 1);
            while (map.size() > k) {
                char chl = s.charAt(l);
                int cntl = map.get(chl) - 1;
                if (cntl == 0) map.remove(chl);
                else map.put(chl, cntl);
                ++l;
            }
            maxLen = Math.max(maxLen, r - l + 1);
            ++r;
        }
        return maxLen;
    }