Given a non-empty string, encode the string such that its encoded length is the shortest.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.

Note:
k will be a positive integer and encoded string will not be empty or have extra space.
You may assume that the input string contains only lowercase English letters. The string's length is at most 160.
If an encoding process does not make the string shorter, then do not encode it. If there are several solutions, return any of them is fine.
Example 1: Input: "aaa"
Output: "aaa"
Explanation: There is no way to encode it such that it is shorter than the input string, so we do not encode it.
Example 2: Input: "aaaaa"
Output: "5[a]"
Explanation: "5[a]" is shorter than "aaaaa" by 1 character.
Example 3: Input: "aaaaaaaaaa"
Output: "10[a]"
Explanation: "a9[a]" or "9[a]a" are also valid solutions, both of them have the same length = 5, which is the same as "10[a]".
Example 4: Input: "aabcaabcd"
Output: "2[aabc]d"
Explanation: "aabc" occurs twice, so one answer can be "2[aabc]d".
Example 5: Input: "abbbabbbcabbbabbbc"
Output: "2[2[abbb]c]"
Explanation: "abbbabbbc" occurs twice, but "abbbabbbc" can also be encoded to "2[abbb]c", so one answer can be "2[2[abbb]c]".

DP:

Initially I think of 1D DP, dp[i] stands for the shortest string of first i characters, then:

dp[i] = minLen{dp[k] + encode(substring(k+1, i))}

then I realize that the second part encode(substring(k+1, i)) is actually the same with our dp problem. So it turns out the transfer function is

dp[i] = minLen{dp[k] + dp(substring(k+1, i))}

then 1D is not enough, I introduce the second dimension, which indicates the end. dp[i][j] is the shortest encoded string from i to j

But the hardest part of this problem is how to generate dp[i][j] from dp[i][k] and dp[k+1][j]

I've thought about the cases like:

dp[i][k] = 3[abc]   dp[k+1][j] = 2[abc],   then dp[i][j] = 5[abc]

dp[i][k] = 3[abc]   dp[k+1][j] = xyz,   then dp[i][j] = 3[abc]xyz

dp[i][k] = aabc   dp[k+1][j] = aabc,   then dp[i][j] = 2[aabc]

No idea what to implement this conveniently, so refer to idea  https://discuss.leetcode.com/topic/71963/accepted-solution-in-java

The idea is to firstly concantenate dp[i][k] and dp[k+1][j] directly to construct dp[i][j], and then check if there exist possible repeat patterns in the original substring s.substring(i, j+1) that could further shorten dp[i][j]

replaceAll function is really clever

Time Complexity is O(N^4), replaceAll() is O(N)

 public class Solution {
public String encode(String s) {
if (s==null || s.length()==0) return "";
String[][] dp = new String[s.length()][s.length()]; for (int len=0; len<s.length(); len++) {
for (int i=0; i+len<s.length(); i++) {
int j = i + len;
String subStr = s.substring(i, j+1);
dp[i][j] = subStr; //initialize
if (len < 4) continue;
for (int k=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];
}
} //check if subStr has repeat pattern
for (int k=i; k<j; k++) {
String repeat = s.substring(i, k+1);
if (subStr.length()%(k-i+1)==0 && subStr.replaceAll(repeat, "").length()==0) {
String ss = subStr.length()/repeat.length() + "[" + dp[i][k] + "]";
if (ss.length() < dp[i][j].length())
dp[i][j] = ss;
}
}
}
}
return dp[0][s.length()-1];
}
}

最新文章

  1. oracleDBA-D3
  2. yii cookie ,session 操作
  3. Xamarin Android -创建Splash Screen (一)
  4. .AndroidRuntimeException: requestFeature() must be called before adding content
  5. 搭建Nginx+JAVA环境
  6. iOS - Alamofire 网络请求
  7. Spring 框架获取 datasource对象的方法
  8. Thinkphp模板中使用自定义函数的方法
  9. Java - 正则表达式常用操作
  10. android开发板
  11. hdu4431 Mahjong
  12. python_将多个小字符拼接成大字符?
  13. mybatis insert 返回主键
  14. Linux grep命令分析以及C语言版本的实现
  15. Linux内核分析--进程创建,执行,切换
  16. SqlServer三种常用窗口函数
  17. Mybatis-generator生成Service和Controller
  18. .net WebService的使用
  19. spark 集成elasticsearch
  20. Highcharts做柱状图怎样样每个柱子都是不同的颜色显示

热门文章

  1. 2012 Multi-University #9
  2. Listener refused the connection with the following error 错误解决
  3. Leetcode Reverse Nodes in k-Group
  4. Android入门(九):CheckBox多选清单和ScrollView滚动条
  5. 【DP】HIHO 1078
  6. 【DP】HDU 1114
  7. vs 2015 连接不上tfs 错误代码:TF31002
  8. 立flag
  9. android 开发项目笔记1
  10. 浏览器对象模型BOM小结