Note:
1. We start with different position and different length to find the shortening.
2. It satisfy following conditions:
I. If length < 4, it short does does help. Number [char] takes four pos.
II. Since shortening happen, we can see whether a substring can is combined by II shortened words. K from [I, J]. Dp[i][k] and Dp[k + 1][j].
III Find all the possible shortening:i) length module should is 0. II) after all the replacement length should is 0.
classSolution { Publicstring Encode (string s) {string[][] DP=Newstring[s.length ()][s.length ()]; for(intL = 0; L < s.length (); l++) { for(inti = 0; I < S.length ()-l; i++) { intj = i +l; String subStr= S.substring (I, j + 1); DP[I][J]=subStr; if(J-i < 4) { Continue; } for(intK = i; K < J; k++) { if((Dp[i][k].length () + dp[k + 1][j].length ()) <dp[i][j].length ()) {Dp[i][j]= Dp[i][k] + dp[k + 1][j]; } } for(intk = 0; K < Substr.length (); k++) {String repeat= Substr.substring (0, K + 1); if(Repeat! =NULL&& substr.length ()% repeat.length () = = 0 && substr.replaceall (Repeat, ""). Length () = = 0) {String result= Substr.length ()/repeat.length () + "[" + Dp[i][i + K] + "]"; if(Result.length () <dp[i][j].length ()) {Dp[i][j]=result; } } } } } returnDp[0][s.length ()-1]; }}
Leetcode 471:encode String with shortest Length