热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Leetcode:EncodeStringwithShortestLength&&G面经

DP:InitiallyIthinkof1DDP,dp[i]standsfortheshorteststringoffirsticharacters,then:dp[i]minLe


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



 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) {
7 for (int i=0; i+len) {
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) {
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) {
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面经



推荐阅读
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Vue 2 中解决页面刷新和按钮跳转导致导航栏样式失效的问题
    本文介绍了如何通过配置路由的 meta 字段,确保 Vue 2 项目中的导航栏在页面刷新或内部按钮跳转时,始终保持正确的 active 样式。具体实现方法包括设置路由的 meta 属性,并在 HTML 模板中动态绑定类名。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文将介绍由密歇根大学Charles Severance教授主讲的顶级Python入门系列课程,该课程广受好评,被誉为Python学习的最佳选择。通过生动有趣的教学方式,帮助初学者轻松掌握编程基础。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
author-avatar
瓦尔登湖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有