当前位置:网站首页>Leetcode 720. The longest word in the dictionary
Leetcode 720. The longest word in the dictionary
2022-06-28 00:28:00 【I'm not xiaohaiwa~~~~】

Give an array of strings words A dictionary of English . return words The longest word in , The word is created by words Other words in the dictionary gradually add a letter to form .
If there are more than one possible answer , The word with the smallest dictionary order in the answer is returned . If there is no answer , Returns an empty string .
Example 1:
Input :words = ["w","wo","wor","worl", "world"]
Output :"world"
explain : word "world" May by "w", "wo", "wor", and "worl" Gradually add a letter to form .
Example 2:
Input :words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
Output :"apple"
explain :"apply" and "apple" Can be composed of words in the dictionary . however "apple" The dictionary order of is less than "apply"
Tips :
- 1 <= words.length <= 1000
- 1 <= words[i].length <= 30
- All input strings words[i] All contain only lowercase letters .
Main idea : Customize sorting first , Then define a two-dimensional array to store words with the same length
Then judge whether the length is continuous , Whether the content is incremented
Code:
class Solution {
public:
static bool cmp(const string &s1,const string &s2)
{
if(s1.length()==s2.length())
return s1<s2;
return s1.length()<s2.length();
}
string longestWord(vector<string>& words) {
sort(words.begin(),words.end(),cmp);
int len=1;
int maxlen=words[words.size()-1].length();
vector<vector<string>>vec;
int start_index=0;
for(int i=1;i<=maxlen;i++)
{
bool flag=false;// Used to judge whether the length is continuous
bool flag2=false;// Used to determine whether the content is incremented
vector<string>temp;
for(int j=start_index;j<words.size();j++)
{
if(words[j].length()==i)
{
flag=true;
//cout<<words[j]<<endl;
if(i!=1)
{
vector<string>sub=vec[i-2];
string t=words[j].substr(0,i-1);
if(find(sub.begin(),sub.end(),t)!=sub.end())
{
// cout<<words[j]<<endl;
temp.push_back(words[j]);
flag2=true;
}
}
if(i==1)
{
flag2=true;
temp.push_back(words[j]);
}
}
else
{
start_index=j;
break;
}
}
if(flag&&flag2)
{
if(i==maxlen)
return temp[0];
}
if(!flag2 || !flag )
{
if(vec.size())
{
return vec[i-2][0];
}
else
return "";
}
else
{
vec.push_back(temp);
}
}
return "";
}
};
边栏推荐
- MATLB|基于复杂网络的配电系统微电网优化配置
- 积分体系和营销活动结合在一起有哪些玩法
- Arduino uno realizes simple touch switch through direct detection of capacitance
- NoSQL之Redis配置与优化
- 什么是cookie,以及v-htm的安全性隐患
- How to quote Chinese documents when writing a foreign language?
- [读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
- [digital ic/fpga] detect the position of the last matching sequence
- CharSequence初探
- Acwing第 57 场周赛【未完结】
猜你喜欢

免费、好用、强大的开源笔记软件综合评测
![Software engineering job design (1): [personal project] implements a log view page](/img/95/0c3f0dde16d220ddecb5758a4c31e7.png)
Software engineering job design (1): [personal project] implements a log view page

Promise是什么

zotero文献管理工具安装使用

Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)

【论文阅读|深读】SDNE:Structural Deep Network Embedding

吴恩达《机器学习》课程总结(14)_降维

Eliminate gaps around El image images

现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence
随机推荐
软件工程作业设计(1): [个人项目] 实现一个日志查看页面
The limits of Technology (11): interesting programming
翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
Redis主从复制、哨兵模式、集群的概述与搭建
券商买股票用什么app是比较好的,比较安全的
mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]
Mongodb- install a mongodb database locally on the windows computer
Translation (4): matching rules for automatic text completion
Count prime [enumeration - > space for time]
C语言malloc函数的功能及用法
Flutter series: Transformers in flutter
Understand the basic structure of wechat applet project
[untitled]
Differences and functions between intranet IP and public IP
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
投资场内ETF基金是靠谱吗,场内ETF基金安全吗
Deployment and test of crtmp live video server
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
Arduino uno realizes simple touch switch through direct detection of capacitance
计数质数[枚举 -> 空间换时间]