LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
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 (字典里最长的单词)的更多相关文章
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- leetcode 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【Leetcode_easy】720. Longest Word in Dictionary
problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- [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 ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
随机推荐
- Selenium基于Python web自动化基础二 -- 免登录、等待及unittest单元测试框架
一.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...
- Mysql慢SQL与索引案例
写在最前 关于慢sql的开启与配置查看之前我整理的文章: http://www.cnblogs.com/hanxiaobei/p/5515624.html 前提准备: tomcat7.x mysql- ...
- Jmeter之计数器
如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现. 计数器(counter):允许用户创建一个在线程组之内都可以被引用的计数器. 计数器允许用户配置一个起点,一个最大值, ...
- docker常用命令理解
docker help Commands: attach Attach local standard input, output, and error streams to a running con ...
- 洛谷——P4071 [SDOI2016]排列计数(错排+组合数学)
P4071 [SDOI2016]排列计数 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列 ...
- C++ volatile关键字(转)
文章来源: http://hedengcheng.com/?p=725 https://www.cnblogs.com/god-of-death/p/7852394.html 1.基本概念 volat ...
- Python学习笔记(3)动态类型
is运算符 ==是值相等而is必须是相同的引用才可以 l=[1,2,3] m=[1,2,3] print(l==m) # True print(l is m) # False sys模块 getref ...
- linux怎么查看已装好硬件驱动
linux系统中的设备驱动是否安装好一般检查几个方面:1.系统日志.嵌入式系统多是直接dmesg一下,看有没有设备关键字相关的出错信息(通用系统可检查/var/log/messages文件).2.已加 ...
- Django-REST_Framework 第三方登录
DRF第三方登录,我们将使用第三方包实现!!! 1.首先安装 pip install social-auth-app-django 文档请看 https://python-social-auth.re ...
- python virtualenv 虚拟环境的应用
为什么要使用python的虚拟环境呢?: 首先我们来说不实用虚拟环境的情况: 在Python应用程序开发的过程中,系统安装的Python3只有一个版本:3.7.所有第三方的包都会被pip3安装到 ...