美文网首页
820. Short Encoding of Words

820. Short Encoding of Words

作者: majinliang123 | 来源:发表于2019-04-17 10:37 被阅读0次

第一次读完这个题目,就感觉这个题目非常之复杂。但是仔细想过之后,就是检测是否长字符串包含短字符串且短字符串是长字符串的结尾部分,如果包含就只保留长字符串。

下面展示一种使用java8 stream解题的方式
基本思路注释在代码里

class Solution {
    public int minimumLengthEncoding(String[] words) {
        return Arrays.asList(words)
            .stream()

            // 将字符串反转,例如原来是"abc",反转之后是"cba"
            .map(StringBuilder::new)
            .map(StringBuilder::reverse)
            .map(StringBuilder::toString)

            // 将所有的字符串倒叙排序,就是说相同前缀的字符串,较长的排在前面
            .sorted(Comparator.reverseOrder())

            // 将字符串按照time#bell#的形式组合在一起,如果后面有字符串符合下面条件,就说明这个字符串是前面某个字符串的后缀。
            .reduce((s1, s2) -> {
                if (s1.contains("#" + s2) || s1.startsWith(s2)) {
                    return s1;
                } else {
                    return s1 + "#" + s2;
                }
            })

            // 最后获取字符串的长度
            .get()
            .length() + 1;
    }
}

相关文章

  • 820. Short Encoding of Words

    第一次读完这个题目,就感觉这个题目非常之复杂。但是仔细想过之后,就是检测是否长字符串包含短字符串且短字符串是长字符...

  • 单词的压缩编码-13.字典树

    820. 单词的压缩编码[https://leetcode.cn/problems/short-encoding-...

  • 2017-12-14

    Life is short. There is no time to leave important words ...

  • Academic Writing Lesson1

    Write a short essay of around 300 words reflecting on you...

  • 『自然拼读天天学』Day(七):单元音练习(Words with

    一、自然拼读:单元音练习(Words with short i & o) 二、词汇拓展:Stories to re...

  • URI Encoding

    What is URL Encoding URL Encoding 也被成为 percent encoding,U...

  • Encoding

    1. 字符发展历史 1.1 字节 计算机内部,所有信息最终都是一个二进制值 每一个二进制位(bit)有0和1两种状...

  • Encoding

    到目前为止,还没有讨论字符串是如何存储的。 字符串由Unicode代码点集合组成。这些代码点的范围从0到11141...

  • Http--Header

    Encoding 请求:Accept-Encoding响应:Content-Encoding 取值:gzip、de...

  • 『字典树』单词的压缩编码820

    题目相关 原题链接:820. 单词的压缩编码 - 力扣(LeetCode) 涉及知识:字典树 题目难度:★★ 题目...

网友评论

      本文标题:820. Short Encoding of Words

      本文链接:https://www.haomeiwen.com/subject/ugdiwqtx.html