剑指 Offer II 065:最短的单词编码
This commit is contained in:
parent
bfead911e6
commit
4b20d65eff
75
src/main/java/leetcode/editor/cn/ISwD2y.java
Normal file
75
src/main/java/leetcode/editor/cn/ISwD2y.java
Normal file
@ -0,0 +1,75 @@
|
||||
//单词数组 words 的 有效编码 由任意助记字符串 s 和下标数组 indices 组成,且满足:
|
||||
//
|
||||
//
|
||||
// words.length == indices.length
|
||||
// 助记字符串 s 以 '#' 字符结尾
|
||||
// 对于每个下标 indices[i] ,s 的一个从 indices[i] 开始、到下一个 '#' 字符结束(但不包括 '#')的 子字符串 恰好与
|
||||
//words[i] 相等
|
||||
//
|
||||
//
|
||||
// 给定一个单词数组 words ,返回成功对 words 进行编码的最小助记字符串 s 的长度 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:words = ["time", "me", "bell"]
|
||||
//输出:10
|
||||
//解释:一组有效编码为 s = "time#bell#" 和 indices = [0, 2, 5] 。
|
||||
//words[0] = "time" ,s 开始于 indices[0] = 0 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"
|
||||
//words[1] = "me" ,s 开始于 indices[1] = 2 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"
|
||||
//words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#"
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:words = ["t"]
|
||||
//输出:2
|
||||
//解释:一组有效编码为 s = "t#" 和 indices = [0] 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= words.length <= 2000
|
||||
// 1 <= words[i].length <= 7
|
||||
// words[i] 仅由小写字母组成
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 注意:本题与主站 820 题相同: https://leetcode-cn.com/problems/short-encoding-of-words/
|
||||
// Related Topics 字典树 数组 哈希表 字符串 👍 13 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
//剑指 Offer II 065:最短的单词编码
|
||||
public class ISwD2y {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new ISwD2y().new Solution();
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int minimumLengthEncoding(String[] words) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
Arrays.sort(words, (o1, o2) -> o2.length() - o1.length());
|
||||
for (String word : words) {
|
||||
if (s.indexOf(word + "#") < 0) {
|
||||
s.append(word).append("#");
|
||||
}
|
||||
}
|
||||
return s.length();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
45
src/main/java/leetcode/editor/cn/doc/content/ISwD2y.md
Normal file
45
src/main/java/leetcode/editor/cn/doc/content/ISwD2y.md
Normal file
@ -0,0 +1,45 @@
|
||||
<p>单词数组 <code>words</code> 的 <strong>有效编码</strong> 由任意助记字符串 <code>s</code> 和下标数组 <code>indices</code> 组成,且满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>words.length == indices.length</code></li>
|
||||
<li>助记字符串 <code>s</code> 以 <code>'#'</code> 字符结尾</li>
|
||||
<li>对于每个下标 <code>indices[i]</code> ,<code>s</code> 的一个从 <code>indices[i]</code> 开始、到下一个 <code>'#'</code> 字符结束(但不包括 <code>'#'</code>)的 <strong>子字符串</strong> 恰好与 <code>words[i]</code> 相等</li>
|
||||
</ul>
|
||||
|
||||
<p>给定一个单词数组 <code>words</code> ,返回成功对 <code>words</code> 进行编码的最小助记字符串 <code>s</code> 的长度 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["time", "me", "bell"]
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>一组有效编码为 s = <code>"time#bell#" 和 indices = [0, 2, 5</code>] 。
|
||||
words[0] = "time" ,s 开始于 indices[0] = 0 到下一个 '#' 结束的子字符串,如加粗部分所示 "<strong>time</strong>#bell#"
|
||||
words[1] = "me" ,s 开始于 indices[1] = 2 到下一个 '#' 结束的子字符串,如加粗部分所示 "ti<strong>me</strong>#bell#"
|
||||
words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#<strong>bell</strong>#"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["t"]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>一组有效编码为 s = "t#" 和 indices = [0] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 2000</code></li>
|
||||
<li><code>1 <= words[i].length <= 7</code></li>
|
||||
<li><code>words[i]</code> 仅由小写字母组成</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" />注意:本题与主站 820 题相同: <a href="https://leetcode-cn.com/problems/short-encoding-of-words/">https://leetcode-cn.com/problems/short-encoding-of-words/</a></p>
|
||||
<div><div>Related Topics</div><div><li>字典树</li><li>数组</li><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 13</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user