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. express+mongodb+mongoose增删改查

    增加 修改 删除 数据库 这是一个前后端分离的项目前端项目地址:https://gitee.com/dingshao/express_qd.git后端项目地址:https://gitee.com/di ...

  2. POJ - 1511 - 两次SPFA

    这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就T了.1e6的数据量,给了8s,网上其他题解中说要用SPFA. 题意:N个点的带权有向图.每次都从1出发,要到达其余没有被访问过的一个点(发传单 ...

  3. Nginx配置性能优化(转)

    原文地址:http://blog.csdn.net/xifeijian/article/details/20956605 高层的配置 nginx.conf文件中,Nginx中有少数的几个高级配置在模块 ...

  4. AB测试的需求

    一.背景 涉及到的项目是 SDK, 用户量 几千万级别,需要调整的是 订单购买整个逻辑,已经发现了低概率问题,影响到的订单不到 0.001% ,但是每周总有几个用户向客服反馈问题.整个逻辑问题,在我入 ...

  5. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  6. js 图表处理之Echar

    官网学习链接:http://echarts.baidu.com/tutorial.html#5%20分钟上手%20ECharts 案例代码: <!DOCTYPE html> <htm ...

  7. jqGrid('setSelection',rowid)报Cannot read property 'multiple' of undefined

    项目组非要上jeeweb框架,用jqgrid+大量iframe做为前端框架,臃肿不堪. 今天上午,在进行选定操作jqGrid('setSelection',rowid)报Cannot read pro ...

  8. APP开放接口API安全性——Token令牌Sign签名的设计与实现

    在APP开放接口API的设计中,避免不了的就是安全性问题. 一.https协议 对于一些敏感的API接口,需要使用https协议.https是在http超文本传输协议加入SSL层,它在网络间通信是加密 ...

  9. linux启动过程⭐

    启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...

  10. Linux升级内核总结

    Linux内核升级总结. 一.编译内核步骤 1.#uname –r 确定系统的原内核版本,然后下载较新版本的Linux内核源码包 http://www.kernel.org/pub/linux/ker ...