Got this online assessment. I came up with a brute force solution where I constantly remove K characters and re-encode the whole string.
It doesn't seem to be efficient to me ... "it seems" that we should use some sort of "Sliding Window" to solve this but I must be missing something. Could someone give a clue?

public class Task2 {
static int solution(String s, int k) {
int kStart = 0, kEnd = k - 1, min = s.length();
if (k == 0) return s.length();
while (kEnd < s.length()) {
String tmp = s.substring(0, kStart) + s.substring(kEnd + 1);
System.out.println("original=" + s + " kStart=" + kStart + " kEnd=" + kEnd + " " + tmp +
" " + encode(tmp));
min = Math.min(encode(tmp).length(), min);
kStart++;
kEnd++;
}
return min;
}
static String encode(String s) {
int count = 1;
StringBuilder ss = new StringBuilder();
for (int i = 1; i <= s.length(); i++) {
if (i == s.length() || s.charAt(i) != s.charAt(i - 1)) {
if (count > 1) {
ss.append(count);
}
ss.append(s.charAt(i - 1));
count = 1;
} else {
count++;
}
}
return ss.toString();
}
public static void main(String[] args) {
//System.out.println(encode("ABCDDDEFG"));
System.out.println("result=" + solution("ABBBCCDDCCC", 3));
System.out.println();
System.out.println("result=" + solution("AAAAAAAAAAABXXAAAAAAAAAA", 3));
System.out.println();
System.out.println("result=" + solution("ABCDDDEFG", 2));
}
}