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

若无答案,则返回空字符串。

示例 1:

输入: words = ["w","wo","wor","worl", "world"] 输出: "world" 解释: 单词"world"可由"w", "wo", "wor", 和 "worl"添加一个字母组成。

示例 2:

输入: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] 输出: "apple" 解释: "apply"和"apple"都能由词典中的单词组成。但是"apple"得字典序小于"apply"。

注意:

  • 所有输入的字符串都只包含小写字母。
  • words数组长度范围为[1,1000]。
  • words[i]的长度范围为[1,30]。
bool cmp1(string a, string b)
{
return a.size() > b.size();
} bool cmp2(string a, string b)
{
return a < b;
} class Solution {
public:
string longestWord(vector<string>& words) {
int len = words.size();
if(len <= 1)
return len == 0? "":words[0];
vector<string> res;
map<string, int> check;
for(int i = 0; i < len; i++)
{
check[words[i]] = 1;
}
sort(words.begin(), words.end(), cmp1);
int MAXsize = 0;
for(int i = 0; i < len; i++)
{
int size = words[i].size();
bool flag = true;
if(MAXsize != 0 && MAXsize > size)
continue;
for(int j = 0; j < size - 1; j++)
{
string temp = "";
for(int k = 0; k <= j; k++)
{
temp += words[i][k];
}
if(check[temp] != 1)
{
flag = false;
break;
}
}
if(flag == true)
{
MAXsize = max(MAXsize, size);
res.push_back(words[i]);
}
}
if(res.size() == 0)
return "";
sort(res.begin(), res.end(), cmp2);
return res[0];
}
};

Leetcode720.Longest Word in Dictionary词典中最长的单词的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Longest Word in Dictionary through Deleting - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...

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

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

  9. 【Leetcode_easy】720. Longest Word in Dictionary

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

随机推荐

  1. sql草稿

    参考:MySQL 内连接.外连接.左连接.右连接.全连接 SELECT count(*) FROM `t_product_base` select m_name from t_medicinal_in ...

  2. Leetcode953. Verifying an Alien Dictionary验证外星语词典

    某种外星语也使用英文小写字母,但可能顺序 order 不同.字母表的顺序(order)是一些小写字母的排列. 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在 ...

  3. 去掉IE提示:internet explorer 已限制此网页运行脚本或Activex控件

    运行加载OCX控件的HTML文件,显示提示如下图: 解决方法是在HTML文件中添加一行注释代码,如下图: 就是红色框内的代码.即:<!-- saved from url=(0014)about: ...

  4. vue 学习 二

    动画 <transition name="fade"> <p v-if="show">hello</p> </tran ...

  5. Python学习之循环--绕圈圈(蛇形盘)

    效果图: 注意哦,右边多出来的一点不是程序有问题,是打印的时候我用的\t,但100,三个字符顶格的时候给顶出去的,我太懒了,不想再调输出格式了,就这么凑合看吧 实现代码: sum = int(inpu ...

  6. System.Timer.Timer的一个安全类

    class SafeTimer { private static System.Timers.Timer timer; public static Action DoWork; private sta ...

  7. LeetCode409Longest Palindrome最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  8. xshell评,xftp估过期解决办法

    去官网 xshell:https://www.netsarang.com/download/down_form.html?code=522 xftp:https://www.netsarang.com ...

  9. mysql本地导入数据

    1.获得一个超级权限的用户 grant all on *.* to root@'127.0.0.1' identified by 'root';# 因为我想在本地导入数据,而数据就在本地.# 有时候, ...

  10. 【html、CSS、javascript-5】css应用场景补充

    一.CSS全局应用 父标签div下包含两个子标签div,当子标签dvi全部向左float,此时父标签设置的背景色是不显示的 <!DOCTYPE html> <html lang=&q ...