题目如下:

解题思路:本题考查就是找出一个单词是不是另外一个单词的后缀,如果是的话,就可以Short Encode。所以,我们可以把words中每个单词倒置后排序,然后遍历数组,每个元素只要和其后面相邻的元素比较,如果是后缀则被Short Encode,否则不行。

代码如下:

class Solution(object):
def minimumLengthEncoding(self, words):
"""
:type words: List[str]
:rtype: int
"""
words2 = sorted([i[::-1] for i in words])
res = 0
for i in range(len(words2)-1):
if words2[i+1].find(words2[i]) == 0:
continue
else:
res += len(words2[i]) + 1
res += len(words2[-1]) + 1
return res

【leetcode】820. Short Encoding of Words的更多相关文章

  1. 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...

  2. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. php 标准库之ArrayObject

    以下为ArrayObject的常用函数: ArrayIterator::current( void ) //返回当前数组元素 ArrayIterator::key(void) //返回当前数组key ...

  2. 深入浅出WPF(Binding篇1)

    Binding在业界的使用一直是音译而来的,称为"Binding".Binding的源是逻辑数据对象,目标则是UI层上面的控件对象.数据通过Binding送达UI层,被UI层展示出 ...

  3. Yaconf – 一个高性能的配置管理扩展

    鸟哥出品:http://www.laruence.com/2015/06/12/3051.html 首先说说, 这个是干啥的. 我见过很多的项目中, 用PHP文件做配置的, 一个config目录下可能 ...

  4. vuejs基础-常见指令(基本结构、v-cloak、v-text、v-html、v-bind、v-model\v-if、v-show)

    Vue之 - 基本的代码结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  5. Linux中ssh及scp的连接

    1. 当你想获取另外一台电脑上的数据时,可以使用这个命令 scp -P 10022 root@172.30.83.173:~/ubuntu1.tar ./ -r   代表传输文件夹,直接传文件可以不加 ...

  6. [SHOI2012] 火柴游戏

    [SHOI2012] 火柴游戏 [题目链接] 链接 [思路要点] 首先发现移动火柴操作可以放到最后做.每一次移动火柴一定可以看做是添加一根火柴再删除一根火柴,并且可以将任意一次添加和一次删除操作合并为 ...

  7. [ARC083]Collecting Balls

    Description 有一个 \(n\times n\) 的矩阵,矩阵内有 \(2n\) 个球.对于 \(i \in [1,n]\) ,\((0,i) (i,0)\) 的位置各有一个启动后往右走/往 ...

  8. 洛谷 P1892 [BOI2003]团伙(种类并查集)

    传送门 解题思路 用并查集f存朋友关系,一个数组e存的是敌人关系,是一个辅助数组,所以叫做种类并查集. 当p和q是朋友时,直接合并,但是当是敌人时,需要一些操作. 当p还没有敌人时(即p的敌人是自己) ...

  9. Matplotlib基础使用

    matplotlib 一.Matplotlib基础知识 Matplotlib中的基本图表包括的元素 x轴和y轴 axis 水平和垂直的轴线 x轴和y轴刻度 tick 刻度标示坐标轴的分隔,包括最小刻度 ...

  10. #python# error:UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30: ordinal not in range(256)

    headers={ 'Referer':'https://www.lagou.com/jobs/lis-rds=&fromSearch=true&suginput=', 'User-A ...