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

题目标签:Hash Table

  题目给了我们一个 words array,让我们找到里面 最长的word,这个word 是由字典里的其他words 由短到长一步步形成。如果遇到两个同样长度的单词,选一个 字母排序靠前的。

  这道题目如果用常规方法做,会比较繁琐,速度也慢。

  可以利用sort words,让所有words排序。

  遍历words,遇到长度等于 1的 word 或者 这个word 去掉最后一个char 已经存在于 set:

    与res 比较 留下 较长的;

    把这个word 加入set。

  因为是排序后的words,所以遍历顺序会是从a 到z, 从短到长。这样就会留下 最长的符合标准的word。也不用考虑 两个同样长度的 words,因为先选的就是 字母排序靠前的。

Java Solution:

Runtime beats 71.19%

完成日期:11/16/2017

关键词:HashSet,sort

关键点:把word 长度为1 的基础单词 加入 set

 class Solution
{
public String longestWord(String[] words)
{
Arrays.sort(words); Set<String> set = new HashSet<>();
String res = ""; for(String word: words)
{
if(word.length() == 1 || set.contains(word.substring(0, word.length() - 1)))
{
res = word.length() > res.length() ? word : res; // keep the longer one
set.add(word);
} } return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/109643/java-c-clean-code

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 720. Longest Word in Dictionary (字典里最长的单词)的更多相关文章

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

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

  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. Leetcode720.Longest Word in Dictionary词典中最长的单词

    给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...

  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] Longest Word in Dictionary 字典中的最长单词

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

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

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

  8. [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 ...

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

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

随机推荐

  1. Python学习日记之中文支持

    解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可

  2. SQL server 上机练习题

    首先创建一个数据库,里面有 登录表 学生表   课程表   选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...

  3. dubbo之日志适配及访问日志

    日志适配 自 2.2.1 开始,dubbo 开始内置 log4j.slf4j.jcl.jdk 这些日志框架的适配 1,也可以通过以下方式显示配置日志输出策略: 命令行 java -Ddubbo.app ...

  4. js删除局部变量

    alert('value:'+str+'\ttype:'+typeof(str)) //声明变量前,引用 var str="dd"; alert('value:'+str+'\tt ...

  5. 数据分片存储,mycat服务器

    关闭防火墙和selinux,配置yum源配置21 .22 数据库(这里以21为例)[root@host21 ~]# tar -xf mysql-5.7.17.tar[root@host21 ~]# y ...

  6. 使用vs2010打开vs2015的项目

    本来在单位项目一直使用vs2010写,五一放假拿回家 ,用vs2015捣鼓了一下 当然向下兼容打开毫无问题,结果回来悲催了,用vs2010打不开了 ,打不开. 记得以前有个转换向导,可是这次没看见,一 ...

  7. 更新dell机器的idrac的固件版本后重启机器系统失败

    事情是这样的.dell ra620机器,idrac7打不开java,所以在机器生产中直接更新了固件,客户直接在系统内reboot后就连不上.打开本地是卡在下图. 强制重启后发现服务器提示,是IDRAC ...

  8. java.lang.NoClassDefFoundError: org/hibernate/validator/internal/engine/DefaultClockProvider

    ①在springboot的spring-boot-starter-web默认引入了以下依赖: <dependency> <groupId>com.fasterxml.jacks ...

  9. 学习SpringBoot中遇见的坑

    1. 在搭建SpringBoot HelloWorld 时项目结构应该这样: 而不能这样: 否则访问时出现错误页面: 原因:此时还不知道,先记录下来. --已解决2018/12/11,因为Spring ...

  10. 22万个木箱!TWaver 3D极限压榨

    打开个门户网站都千呼万唤,我们还能期待网页上的3D技术会有“酣畅淋漓”.“一气呵成”的感受吗?也许现在还差点火候.但是HTML5.WebGL等技术一直在飞速的发展,可能很快你就会惊讶它的能力.现在,我 ...