import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param k int整型
* @return int整型
*/
public int longestSubstring (String s, int k) {
// write code here
int l = 0;
int r = 0;
int res = 0;
HashSet<Character> hashSet = new HashSet<>();
while (l < s.length()) {
r = l;
hashSet.clear();
while (hashSet.size() <= k && r < s.length()) {
hashSet.add(s.charAt(r));
r++;
}
if (r == s.length()) {
if (hashSet.size() <= k) {
res = Math.max(res, r - l);
}
if (hashSet.size() - 1 == k) {
res = Math.max(res, r - l - 1);
}
break;
}
res = Math.max(res, r - l - 1);
l++;
}
return res;
}
}