import java.util.*;
/**
 * Given a string, find the length of the longest substring T that contains at most k distinct characters. 
 * For example, given s = "ecceba" and k = 2,
 * T is "ece" which has a length of 3 and 2 distinct characters.
 */
public class Solution {
  public static int lengthOfLongestSubstringKDistinct(String s, int k) {
    if (s.length() == 0 || s.length() == 1) return s.length();
    if (k == 0) return 0;

    int i = 0; 
    int j = 0;
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    int distinct = 0;
    int maxLength = 0;

    while (j < s.length()) {
      char next = s.charAt(j);
      int count = map.get(next) != null && map.get(next) > 0 ? map.get(next) : 0;
      map.put(next, count + 1);
      if (count == 0) distinct ++;

      // The number of distinct character is equal to K so we must remove the a distinct character and all of its occurences.
      if (distinct > k) {
        char start = s.charAt(i);

        // Keep moving the beginning pointer until a character frequency is 0
        while (i < j && map.get(s.charAt(i)) != null && map.get(s.charAt(i)) - 1 > 0) {
          map.put(s.charAt(i), map.get(s.charAt(i++) - 1));
        }

        // Remove the last character.
        map.put(s.charAt((i++) - 1), 0);
        distinct --;
      }

      maxLength = Math.max(maxLength, j - i + 1);
      j ++;
    }

    return maxLength;
  } 
}

/**
 * Given a string, find the length of the longest substring T that contains at most k distinct characters. 
 * For example, given s = "ecceba" and k = 2,
 * T is "ece" which has a length of 3 and 2 distinct characters.
 */
public class Solution {
  public static int lengthOfLongestSubstringKDistinct(String s, int k) {
    if (k == 0) return 0;
    if (s.length() == 0 || s.length() == 1) return s.length();

    int i = 0; 
    int j = 0;
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    int distinct = 0;
    int maxLength = 0;

    while (j < s.length()) {
      char next = s.charAt(j);
      int count = map.get(next) != null && map.get(next) > 0 ? map.get(next) : 0;
      map.put(next, count + 1);
      if (count == 0) distinct ++;

      // The number of distinct character is equal to K so we must remove the a distinct character and all of its occurences.
      if (distinct > k) {
        char start = s.charAt(i);

        // Keep moving the beginning pointer until a character frequency is 0
        while (i < j && map.get(s.charAt(i)) != null && map.get(s.charAt(i)) - 1 > 0) {
          map.put(s.charAt(i), map.get(s.charAt(i++) - 1));
        }

        // Remove the last character.
        map.put(s.charAt((++i) - 1), 0);
        distinct --;
      }

      maxLength = Math.max(maxLength, j - i + 1);
      j ++;
    }

    return maxLength;
  } 
}

Fix changes