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].

排序+set解法

class Solution(object):
def longestWord(self, words):
"""
:type words: List[str]
:rtype: str
"""
# use greey algo
# find the most length word that can be built one character at a time by other words in words
words_set = set([""])
words.sort()
ans = ""
for word in words:
if word[:-1] in words_set:
if len(word) > len(ans):
ans = word
words_set.add(word)
return ans

或者是trie:

class Node(object):
def __init__(self, val=""):
self.val = val
self.subs = collections.defaultdict(Node) class Trie(object):
def __init__(self):
self.root = Node("") def insert(self, s):
node = self.root
for c in s:
node = node.subs[c]
node.val = s def longest_word(self):
self.ans = ""
def dfs(node):
for k, n in node.subs.items():
if n.val:
if len(n.val)>len(self.ans) or (len(n.val)==len(self.ans) and n.val<self.ans):
self.ans = n.val
dfs(n)
dfs(self.root)
return self.ans class Solution(object):
def longestWord(self, words):
"""
:type words: List[str]
:rtype: str
"""
trie = Trie()
for word in words:
trie.insert(word)
return trie.longest_word()

leetcode 720. Longest Word in Dictionary的更多相关文章

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

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

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

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

  3. 【Leetcode_easy】720. Longest Word in Dictionary

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

  4. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

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

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

  6. [LeetCode&Python] Problem 720. Longest Word in Dictionary

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

  7. #Leetcode# 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...

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

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

  9. leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词

    一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...

随机推荐

  1. javaScript动画2 scroll家族

    offsetWidth和offsetHight (检测盒子自身宽高+padding+border) 这两个属性,他们绑定在了所有的节点元素上.获取之后,只要调用这两个属性,我们就能够获取元素节点的宽和 ...

  2. 使用tar解压文件报归档中找不到

    1.今天使用tar命令解压jdk安装包时,报如下错误.tar -zxvf jdk-8u181-linux-x64.tar.gz /usr/local/java/ 2.后来查了一下,因为我解压当前的文件 ...

  3. FFmpeg 入门(3):播放音频

    本文转自:FFmpeg 入门(3):播放音频 | www.samirchen.com 音频 SDL 提供了播放音频的方法.SDL_OpenAudio 函数用来让设备播放音频,它需要我们传入一个包含了所 ...

  4. Python面试题之Python反射机制

    0x00 前言 def f1(): print('f1') def f2(): print('f2') def f3(): print('f3') def f4(): print('f4') a = ...

  5. BeanFactory与ApplicationContext

    本文总结自:https://www.cnblogs.com/xiaoxi/p/5846416.html 我们常说的Spring容器(即Spring Ioc 容器),是如何创建bean的? BeanFa ...

  6. 20145310 《Java程序设计》第6周学习总结

    20145310 <Java程序设计>第6周学习总结 教材学习内容总结 本周主要进行第十章和第十一章的学习. 第十章 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流 ...

  7. git操作方便,简单使用的客户端sourcetree 安装配置所遇问题总结

    常言道:工欲善其事,必先利其器. SourceTree是老牌的Git GUI管理工具了,也号称是最好用的Git GUI工具 这里先言言它的好: * 免费 * 功能强大:无论你是新手还是重度用户,Sou ...

  8. 【转载】JExcelApi(JXL)学习笔记

    在公司的项目中,有excel生成.导出的需求,因此学习了用JXL读写excel,做个简单的笔记,以供参考.      实现用java操作excel的工具,一般用的有两个:一个是JXL,另一个是apac ...

  9. 源码安装GCC-4.9.2

    本文参考:http://cuchadanfan.blog.51cto.com/9940284/1689556  感谢原作者的分享! 首先安装基础包,安装网络依赖的时候要用 [root@localhos ...

  10. 解决"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站"的问题

    在ASP.NET MVC项目中,使用AJAX向控制器发送GET请求获取JSON数据时,出现这个错误:"此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站.若要允许 G ...