classSolution{publicintgetLengthOfOptimalCompression(Strings,intk){// dp[i][k] := the length of the optimal compression of s[i..n) with at most// k deletiondp=newint[s.length()][k+1];Arrays.stream(dp).forEach(A->Arrays.fill(A,K_MAX));returncompression(s,0,k);}privatestaticfinalintK_MAX=101;privateint[][]dp;privateintcompression(finalStrings,inti,intk){if(k<0){returnK_MAX;}if(i==s.length()||s.length()-i<=k){return0;}if(dp[i][k]!=K_MAX){returndp[i][k];}intmaxFreq=0;int[]count=newint[128];// Make letters in s[i..j] be the same.// Keep the letter that has the maximum frequency in this range and remove// the other letters.for(intj=i;j<s.length();++j){maxFreq=Math.max(maxFreq,++count[s.charAt(j)]);dp[i][k]=Math.min(dp[i][k],getLength(maxFreq)+compression(s,j+1,k-(j-i+1-maxFreq)));}returndp[i][k];}// Returns the length to compress `maxFreq`.privateintgetLength(intmaxFreq){if(maxFreq==1){return1;// c}if(maxFreq<10){return2;// [1-9]c}if(maxFreq<100){return3;// [1-9][0-9]c}return4;// [1-9][0-9][0-9]c}}