// time:O(n) space:O(128) ~ O(1) publicstatic String getStringNoMoreKDistinct(String s, int k){ if (s == null || s.length() == 0) return""; int[] counter = newint[128]; int end = 0; int start = 0; int num = 0; int max = 0; String res = ""; while (end < s.length()) { if (counter[s.charAt(end)]++ == 0) num++; while (num > k) { counter[s.charAt(start)]--; if (counter[s.charAt(start)] == 0) num--; start++; } end++; if (end - start > max) { max = end - start; res = s.substring(start, end); } } return res; }