作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-word-in-dictionary/description/

题目描述

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:

  1. All the strings in the input will only contain lowercase letters.
  2. The length of words will be in the range [1, 1000].
  3. 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
"""
wset = set(words)
res = ""
for w in words:
isIn = True
for i in range(1, len(w)):
if w[:i] not in wset:
isIn = False
break
if isIn:
if not res or len(w) > len(res):
res = w
elif len(w) == len(res) and res > w:
res = w
return res

排序

巧妙用了set(),判断每个单词的除去倒数第一个字母是否在set里,用一个变量保存最长的单词。

用判断新单词是否比最长单词更长的方式完成两个需求:1.找出最长;2.同样最长的情况下,保留字母序最小的。这样做的前提是先对words进行排序。

class Solution(object):
def longestWord(self, words):
"""
:type words: List[str]
:rtype: str
"""
words.sort()
res = set([''])
longestWord = ''
for word in words:
if word[:-1] in res:
res.add(word)
if len(word) > len(longestWord):
longestWord = word
return longestWord

日期

2018 年 1 月 21 日
2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】720. Longest Word in Dictionary 解题报告(Python)的更多相关文章

  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

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

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

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

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

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

  5. 【Leetcode_easy】720. Longest Word in Dictionary

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

  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】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

随机推荐

  1. Oracle-oracle中union和union all的区别

    union和union all的区别Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序:Union All:对两个结果集进行并集操作,包括重复行,不进行排序: union和un ...

  2. keras房价预测数据集boston_housing.npz文件下载

    github地址: https://github.com/yuxiwang66/boston_housing 码云地址: https://gitee.com/polarisslk/boston_hou ...

  3. 在windows 10家庭版上安装docker的步骤

    本人之前写Redis书和Spring Cloud Alibaba书时,发现一些分布式组件更适合安装在linux环境,而在搭建Redis等集群时,更需要linux环境. 本人日常练习代码和写书所用的机器 ...

  4. day13 iptables防火墙

    day13 iptables防火墙 一.防火墙的概述 1.什么是防火墙 防止恶意流量访问的软件就叫做防火墙. 2.防火墙的种类 软件防火墙:firewalld.iptables 硬件防火墙:F5 fi ...

  5. 如何通过 User-Agent 识别百度蜘蛛

    如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...

  6. RAC中常见的高级用法-组合

    组合: concat组合:           按一定顺序执行皇上与皇太子关系 concat底层实现:     1.当拼接信号被订阅,就会调用拼接信号的didSubscribe     2.didSu ...

  7. Shell脚本实现根据文件的修改时间来分类文件

    #!/bin/bash # exctute # ./mod.sh file_type input_folder output_folder # ./mod.sh *.txt /tmp /data/ # ...

  8. 【Linux】【Services】【SaaS】Docker+kubernetes(9. 安装consul实现服务注册发现)

    1. 简介 1.1. 官方网站: https://www.consul.io 1.2. Consul的功能: 服务发现:通过DNS或HTTP接口使得消费者发现服务,应用程序可以轻松找到所依赖的服务. ...

  9. html之table的tr加间隔

    <table style="border-collapse:separate; border-spacing:0px 10px;"> <tr> <td ...

  10. mysq中char,varchar,text的区别

    mysql5.0.3以后,n都表示字符数(varchar(n)) 检索效率 char > varchar > text 当varchar长度超过255之后,跟text一致,但是设置varc ...