问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

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

  2. Java实现 LeetCode 720 词典中最长的单词(字典树)

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

  3. Leetcode字典树-720:词典中最长的单词

    第一次做leetcode的题目,虽然做的是水题,但是菜鸟太菜,刚刚入门,这里记录一些基本的知识点.大佬看见请直接路过. https://leetcode-cn.com/problems/longest ...

  4. leetcode 720. 词典中最长的单词

    /* 1.hashtable 把每个字符串都放到hashtable中 a.排序 长度不同,长的放在前面,长度相同,字典序小的放在前面 b.不排序 遍历数组,对于每个字符串判断它的所有前缀是否都在has ...

  5. Leetcode720.Longest Word in Dictionary词典中最长的单词

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

  6. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  7. C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...

  8. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

  9. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

随机推荐

  1. 一分钟部署nacos

    第一步:下载nacos包 https://github.com/alibaba/nacos/releases  D:\testNacos\nacos-server-1.3.0\nacos\bin 最后 ...

  2. 怎样才能做好软件测试——Python自动化测试工程师七年感悟

    即使不想在文章的开头过分的正经严肃,但这是一个十分正经技术类规划类的分享.不讲笑话也不讲故事,直接进入主题. 如何学好软件测试?反推一下作为一名优秀的软件测试工程师需要什么能力.   学习测试讲究实践 ...

  3. 因为mac不支持移动硬盘的NTFS格式,mac电脑无法写入移动硬盘的终极解决办法

    相信很多实用mac的同学,都有磁盘容量问题,所以才使用移动硬盘 当移动硬盘在windows电脑上使用过之后,会被格式化为NTFS格式 而mac电脑不支持NTFS格式 这里有两种方法 第一种是把移动硬盘 ...

  4. STL源码剖析:序列式容器

    前言 容器,置物之所也.就是存放数据的地方. array(数组).list(串行).tree(树).stack(堆栈).queue(队列).hash table(杂凑表).set(集合).map(映像 ...

  5. C++语法小记---标准库

    C++标准库 C++标准库包含如下内容: C++标准编译工具链 C++扩展编译工具链(各种C++编译器独有) C++标准库 C++库 C库 C兼容库(为了兼容能够用C编译器编译的项目,直接使用C++也 ...

  6. 前端练习错题day01

    <1>.css盒子模型中,padding是透明的,这一部分可以显示背景. <2>.注意&&符号左右条件先后顺序,可能会报错. <3>.在 HTML5 ...

  7. super,this关键字用法 Java

    super 用法 1.调用父类变量2.调用父类方法3.子类构造方法第一句 this 用法 super关键字用来访问父类内容, this 关键字用来访问本类中的内容, 有三种用法 1.在本类的成员方法中 ...

  8. F - Maximal Intersection --------暴力求解题

    You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...

  9. [C++]类的空指针调用成员函数后,会发生什么事?

    类的实例调用成员函数的原理 其实不管是通过对象实例或指针实例调用,其实底层调用的过程都是一样的,都是把当前对象的指针作为一个参数传递给被调用的成员函数.通过下面的相关实例代码进行检验: 实验的C++代 ...

  10. day8.列表、字典、集合相关操作

    一.列表的相关操作 # (1)列表的拼接 (同元组) # (2)列表的重复 (同元组) # (3)列表的切片 (同元组) # 语法 => 列表[::] 完整格式:[开始索引:结束索引:间隔值] ...