C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4120 访问。
给出一个字符串数组words组成的一本英语词典。从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成。若其中有多个可行的答案,则返回答案中字典序最小的单词。
若无答案,则返回空字符串。
输入: words = ["w","wo","wor","worl", "world"]
输出: "world"
解释: 单词"world"可由"w", "wo", "wor", 和 "worl"添加一个字母组成。
输入: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
输出: "apple"
解释: "apply"和"apple"都能由词典中的单词组成。但是"apple"得字典序小于"apply"。
注意:
所有输入的字符串都只包含小写字母。
words数组长度范围为[1,1000]。
words[i]的长度范围为[1,30]。
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.
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".
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].
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4120 访问。
public class Program {
public static void Main(string[] args) {
var words = new string[] { "a", "banana", "app", "appl", "ap", "apply", "apple" };
var res = LongestWord(words);
Console.WriteLine(res);
Console.ReadKey();
}
private static string LongestWord(string[] words) {
var wordsSet = new HashSet<string>();
var resultSet = new HashSet<string>();
Array.Sort(words);
for(var i = 0; i < words.Length; i++) {
wordsSet.Add(words[i]);
}
for(var i = words.Length - 1; i >= 0; i--) {
if(IsCompleteWord(words[i], wordsSet)) {
resultSet.Add(words[i]);
}
}
var list = resultSet.OrderByDescending(r => r.Length).ToList();
return list.Where(r => r.Length == list[0].Length).Min();
}
private static bool IsCompleteWord(string word, HashSet<string> wordsSet) {
for(var i = 0; i < word.Length; i++) {
if(!wordsSet.Contains(word.Substring(0, i + 1))) return false;
}
return true;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4120 访问。
apple
分析:
设单词的最大长度为m,那么以上算法的时间复杂度应为: 。
C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)的更多相关文章
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Java实现 LeetCode 720 词典中最长的单词(字典树)
720. 词典中最长的单词 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最 ...
- Leetcode字典树-720:词典中最长的单词
第一次做leetcode的题目,虽然做的是水题,但是菜鸟太菜,刚刚入门,这里记录一些基本的知识点.大佬看见请直接路过. https://leetcode-cn.com/problems/longest ...
- leetcode 720. 词典中最长的单词
/* 1.hashtable 把每个字符串都放到hashtable中 a.排序 长度不同,长的放在前面,长度相同,字典序小的放在前面 b.不排序 遍历数组,对于每个字符串判断它的所有前缀是否都在has ...
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
- C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
随机推荐
- 毕业三年从月薪6K到20K
首先,声明这不是标题党,是一个真实的北漂故事! 为什么写这篇文章呢?第一,有感而发,感恩遇到的人和事,其次,希望对读这篇文章的你有所帮助 毕业那年 时间追溯到17年6月30号,那天毕业典礼,之 ...
- Goorm永久免费的VPS
简介 咱在LOC(某知名主机论坛)看到的,当个容器玩玩还是不错的,不过10分钟后会休眠,目前还没有大佬可以解决,可以使用SSH连接,适合折腾,不适合建站等生产环境操作,请注意.https://www. ...
- 高效C++:定制new和delete
内存的申请和释放,C++从语言级别提供了new和delete关键字,因此需要了解和熟悉其中的过程. 了解new-handler的行为 set_new_handler可以指定一个函数,当申请内存失败时调 ...
- Selenium自动化:有代码测试与无代码测试。这些你都懂了吗?
大多数测试人员认为 Selenium是满足其测试自动化需求的自动化框架.作为全球测试人员使用的开放源框架, Selenium 无疑是测试人员适应日趋敏捷的公司的一种好方法.实际上, Selenium仍 ...
- 目前解决移动端1px边框最好的方法
在移动端开发时,经常会遇到在视网膜屏幕中元素边框变粗的问题.本文将带你探讨边框变粗问题的产生原因及介绍目前市面上最好的解决方法. 1px 边框问题的由来 苹果 iPhone4 首次提出了 Retina ...
- Java线程的6种状态及切换
Java中线程的状态分为6种. 1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法.2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running) ...
- SAS X option
1. SAS X选项就是调用DOS命令. 例子: option noxwait;/*黑窗口执行完命令后自动关闭*/ %let path =.; %let filter=*.lst; X “ dir & ...
- Spark 3.0 新特性 之 自适应查询与分区动态裁剪
Spark憋了一年半的大招后,发布了3.0版本,新特性主要与Spark SQL和Python相关.这也恰恰说明了大数据方向的两大核心:BI与AI.下面是本次发布的主要特性,包括性能.API.生态升级. ...
- python的常用模块
一.random随机数模块 使用随机数模块需要导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生 ...
- laravel 数据分页简单示例
控制器代码:只需用paginate($pageSize)方法查询数据即可 $pageSize:每页显示的记录数 public function index() { $data = Member::pa ...