Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.

If there is no answer, return the empty string.

Example 1:

Input:
words = ["w","wo","wor","worl", "world"]
Output: "world"
Explanation:
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".

Example 2:

Input:
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
Output: "apple"
Explanation:
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".

Note:

  • All the strings in the input will only contain lowercase letters.
  • The length of words will be in the range [1, 1000].
  • The length of words[i] will be in the range [1, 30].
class Solution(object):
def longestWord(self, words):
"""
:type words: List[str]
:rtype: str
"""
ans=""
wordset=set(words)
for w in words:
if len(w)>len(ans) or (len(w)==len(ans) and w<ans):
if all(w[:k] in wordset for k in range(1,len(w))):
ans=w
return ans

  

[LeetCode&Python] Problem 720. Longest Word in Dictionary的更多相关文章

  1. 【Leetcode_easy】720. Longest Word in Dictionary

    problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...

  2. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  3. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. leetcode 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  5. 720. Longest Word in Dictionary 能连续拼接出来的最长单词

    [抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...

  6. [leetcode]720. Longest Word in Dictionary字典中最长的单词

    b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...

  7. [LeetCode&Python] Problem 594. Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  8. [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...

  9. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

随机推荐

  1. 5、zabbix使用进阶(01)

    详细描述user parameters.定义主机发现规则实现自动发现.如何定义和实现自动注册方式 zabbix常用术语 1.主机(host):要监控的网络设备,可有IP或DNS名称指定: 2.主机组( ...

  2. 5_bash

    bash及其特性:shell:外壳.用户直接接入计算机的时候所使用的外壳程序linux允许一个用户账户登录多次,而这多次登录的每一个打开的shell都是独立的互不相干的shell,它们是三个进程,每一 ...

  3. 利用python操作excel

    https://zhuanlan.zhihu.com/p/51292549 打开程序:https://segmentfault.com/q/1010000002441500

  4. DAY9 函数

    一.脚本文件的执行 1.存放当前文件作为脚本文件执行的参数们:[‘当前文件的绝对路径’,手动传入的参数们] 2.脚本文件执行:直接用python解释器运行该文件  print(sys.argv) # ...

  5. UGUI之导入图片之前自动设置图片打包的 tag

    之前一直在用的是NGUI,最近不知怎么突然兴趣来潮,想学习一下UGUI,毕竟,现在纵观Unity的市场,完全是UGUI的天下,NGUI已经渐渐退隐江湖,哈哈哈... 先来记录下,在图片资源导入到Uni ...

  6. zookeeper: zkServer.sh status没有到主机的路由

    zookeeper: zkServer.sh status没有到主机的路由 没有到主机的路由这种问题很常见,多数是由机器的防火墙没有关闭. Ubuntu查看防火墙状态ufw status 关闭防火墙u ...

  7. ranch 源码分析(二)

    接上ranch 源码分析(一) 上次讲到了ranch.erl的start_listener函数,下面我们详细分析下这个函数 -module(ranch). %...... 省略若干行 -spec st ...

  8. input 文本框自动显示光标

    使用$("#votetitle").focus();没起作用 使用document.getElementById("votetitlechild").focus ...

  9. sparse_tensor feed_dict的时候十分不方便。

    假如说,你再处理文本的时候,写tfrecord的时候用的变长的类型, example = tf.train.Example(features=tf.train.Features(feature={ ' ...

  10. PAT 1069 The Black Hole of Numbers

    1069 The Black Hole of Numbers (20 分)   For any 4-digit integer except the ones with all the digits ...