当前位置:网站首页>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 "";
}
};
边栏推荐
- 快速掌握grep命令及正则表达式
- MySQL enterprise parameter tuning practice sharing
- Does the subscription of Siyuan notes stop deleting cloud data directly?
- Sentinel
- Thread pool implementation: semaphores can also be understood as small waiting queues
- Alchemy (9): simple but not simple, never-ending test -- always_ run
- Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
- 吴恩达《机器学习》课程总结(11)_支持向量机
- IIC communication protocol for single chip microcomputer
- Code neatness -- format
猜你喜欢
安全省油環保 駱駝AGM啟停電池魅力十足
Sword finger offer 61 Shunzi in playing cards
炼金术(1): 识别项目开发中的ProtoType、Demo、MVP
Msp430f5529 MCU reads gy-906 infrared temperature sensor
Matlb| optimal configuration of microgrid in distribution system based on complex network
GFS 分布式文件系统概述与部署
Modern programming languages: zig
Sell notes | brief introduction to video text pre training
本地可视化工具连接阿里云centOS服务器的redis
Modern programming language: rust
随机推荐
Function and usage of malloc function in C language
MongoDB-在windows电脑本地安装一个mongodb的数据库
Msp430f5529 MCU reads gy-906 infrared temperature sensor
剑指 Offer 61. 扑克牌中的顺子
Recyclerview implements grouping effects in a variety of ways
2022 PMP project management examination agile knowledge points (3)
股市小白在网上股票开户安全吗?
Sentinel
線程池實現:信號量也可以理解成小等待隊列
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
Modern programming languages: zig
Alchemy (1): identify prototype, demo and MVP in project development
It supports deleting and updating the priority queue of any node
ValidateRequest=”false” 是做什么的「建议收藏」
赛尔笔记|视频文本预训练简述
RecyclerView实现分组效果,多种实现方式
MATLAB basic function length function
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
Understand the basic structure of wechat applet project
自定义MySQL连接池