1. Given a non-empty string, encode the string such that its encoded length is the shortest.
  2.  
  3. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.
  4.  
  5. Note:
  6. k will be a positive integer and encoded string will not be empty or have extra space.
  7. You may assume that the input string contains only lowercase English letters. The string's length is at most 160.
  8. 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.
  9. Example 1:
  10.  
  11. Input: "aaa"
  12. Output: "aaa"
  13. Explanation: There is no way to encode it such that it is shorter than the input string, so we do not encode it.
  14. Example 2:
  15.  
  16. Input: "aaaaa"
  17. Output: "5[a]"
  18. Explanation: "5[a]" is shorter than "aaaaa" by 1 character.
  19. Example 3:
  20.  
  21. Input: "aaaaaaaaaa"
  22. Output: "10[a]"
  23. 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]".
  24. Example 4:
  25.  
  26. Input: "aabcaabcd"
  27. Output: "2[aabc]d"
  28. Explanation: "aabc" occurs twice, so one answer can be "2[aabc]d".
  29. Example 5:
  30.  
  31. Input: "abbbabbbcabbbabbbc"
  32. Output: "2[2[abbb]c]"
  33. 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)

  1. public class Solution {
  2. public String encode(String s) {
  3. if (s==null || s.length()==0) return "";
  4. String[][] dp = new String[s.length()][s.length()];
  5.  
  6. for (int len=0; len<s.length(); len++) {
  7. for (int i=0; i+len<s.length(); i++) {
  8. int j = i + len;
  9. String subStr = s.substring(i, j+1);
  10. dp[i][j] = subStr; //initialize
  11. if (len < 4) continue;
  12. for (int k=i; k<j; k++) {
  13. if (dp[i][k].length() + dp[k+1][j].length() < dp[i][j].length()) {
  14. dp[i][j] = dp[i][k] + dp[k+1][j];
  15. }
  16. }
  17.  
  18. //check if subStr has repeat pattern
  19. for (int k=i; k<j; k++) {
  20. String repeat = s.substring(i, k+1);
  21. if (subStr.length()%(k-i+1)==0 && subStr.replaceAll(repeat, "").length()==0) {
  22. String ss = subStr.length()/repeat.length() + "[" + dp[i][k] + "]";
  23. if (ss.length() < dp[i][j].length())
  24. dp[i][j] = ss;
  25. }
  26. }
  27. }
  28. }
  29. return dp[0][s.length()-1];
  30. }
  31. }

Leetcode: Encode String with Shortest Length && G面经的更多相关文章

  1. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

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

  2. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  3. LeetCode : Given a string, find the length of the longest serial substring without repeating characters.

    Given a string, find the length of the longest serial substring without repeating characters. Exampl ...

  4. Leetcode: Encode and Decode TinyURL

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  5. Leetcode 943. Find the Shortest Superstring(DP)

    题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...

  6. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  7. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  8. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  9. 数组有没有length()这个方法? String有没有length()这个方法?

    答:数组和string都没有Length()方法,只有Length属性.

随机推荐

  1. nodejs review-03

    39 Serve different file types with our server 处理文件类型 function content_type(filename) { var ext = pat ...

  2. BZOJ 2115 [Wc2011] Xor ——线性基

    [题目分析] 显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可. 但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有 ...

  3. EXCEL处理大量数据的潜在风险

    同事收到几份60几M的xls文件,电脑性能不够,发给我来处理. 处理发现有BUG.简单的vlookup,如果只是实验性的处理几个数据的话,发现没有问题,但批量对全部数据进行处理,就会出现#N/A的问题 ...

  4. uri不能处理结尾为点的url的问题

    最近需要和某公司进行接口对接,发现用WebClient获取URL结尾带.的资源,会出404错误.但是用IE还有其它浏览器访问此资源,还能找到它.很神奇. 于是,我百度了,找到的一堆都是说此url不规范 ...

  5. SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  6. 阿里云SLB双机IIS多站点负载均衡部署笔记

    首先SLB是通过局域网与ECS链接 ECS1服务器 test文件夹增加index.html test1文件夹增加index.html 设置ECS1服务器(130)IIS test站点 设置test主机 ...

  7. Android 上传图片并添加参数 PHP接收

    php端接收代码: public function get_file(){ $local_path = "./Public/daixu_picture/figure/";//服务器 ...

  8. VMware与virtualbox安装centos7连接网络不可达问题解决笔记(连接网络)

    我最初是安装vmware遇到访问不到网络,按网上的配置方法都不能解决.然后我感觉可能跟系统有关,我装的是centos,然后我试着在virtualbox上安装看遇到什么问题. 用virtualbox安装 ...

  9. centos7 最小化安装没有ifconfig及修改网卡名enoxxx为ethX

    问题: 1.最小化安装centos7后发现无ifconfig命令 想通过ifconfig查看ip地址发现ifconfig命令不存在,可通过命令 #ip addr       //查看ip 或者 解决: ...

  10. Meteor 学习

    官方网站 https://www.meteor.com/ 官方API手册 https://guide.meteor.com/ http://docs.meteor.com/ 中文网站 http://c ...