当前位置:网站首页>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 "";
}
};
边栏推荐
- Feign implements path escape through custom annotations
- RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
- Alchemy (4): mental model of programmers
- 吴恩达《机器学习》课程总结(14)_降维
- 投资场内ETF基金是靠谱吗,场内ETF基金安全吗
- 炼金术(3): 怎样做好1个业务流程的接口对接
- 表单form 和 表单元素(input、select、textarea等)
- Matlab基本函数 length函数
- 数仓的字符截取三胞胎:substrb、substr、substring
- Eliminate gaps around El image images
猜你喜欢

MySQL企业级参数调优实践分享

MySQL分表查询之Merge存储引擎实现

现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)

The limits of Technology (11): interesting programming

Redis主从复制、哨兵模式、集群的概述与搭建

免费、好用、强大的开源笔记软件综合评测
最新MySQL高级SQL语句大全
![[idea] idea formatting code skills](/img/06/38079517e901bc48dc4ca0f8cc63fe.jpg)
[idea] idea formatting code skills

Sécurité, économie de carburant et protection de l'environnement chameau

zotero文献管理工具安装使用
随机推荐
Count prime [enumeration - > space for time]
Customize MySQL connection pool
RNA SEQ introduction practice (I): upstream data download, format conversion and quality control cleaning
A summer party
Introduction to data warehouse
免费、好用、强大的开源笔记软件综合评测
TIME_ Solutions to excessive wait
Flutter series: Transformers in flutter
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
What is promise
炼金术(3): 怎样做好1个业务流程的接口对接
夏日的晚会
Sentinel
Summary of wuenda's machine learning course (11)_ Support vector machine
MATLAB basic function length function
一个人可以到几家证券公司开户?开户安全吗
Logging log usage
单片机之IIC通信协议「建议收藏」
炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
吴恩达《机器学习》课程总结(13)_聚类