当前位置:网站首页>Leetcode 720. the longest word in the dictionary
Leetcode 720. the longest word in the dictionary
2022-07-22 18:49:00 【Great white sheep_ Aries】
Title Description
720. The longest word in the dictionary
solution : The meter
Let's put the words Sort by word length , Those with the same length shall be sorted according to the requirements of the topic and the order of the dictionary , Final words It can ensure that every step in the traversal process is the longest at present 、 The word with the smallest dictionary order
If the word currently traversed is deleted, the last letter can be found in the table , Then it shows that the current word meets the requirements of the answer , Then you can update the answer , Insert it into the table
class Solution {
public:
string longestWord(vector<string>& words) {
sort(words.begin(), words.end(), [](const string& a, const string& b){
return a.size() != b.size() ? a.size() < b.size() : a>b;
});
string ans;
unordered_set<string> candidates = {
""};
for (auto word: words)
{
if (candidates.count(word.substr(0, word.size() - 1)))
{
candidates.emplace(word);
ans = word;
}
}
return ans;
}
};
Solution 2 : Dictionary tree
This problem is modified on the basis of the dictionary tree search
You can get : At the beginning , We will all word Insert into the dictionary tree , Then the prefix node of each candidate answer that meets the requirements of the question must meet isEnd = true, Next, compare the length and dictionary order to get the longest word
class Trie {
private:
bool isEnd;
Trie* next[26];
public:
/** Initialize your data structure here. */
Trie() {
isEnd = false;
memset(next, 0, sizeof(next));
}
/** Inserts a word into the trie. */
void insert(string word) {
Trie* node = this;
for(auto c: word)
{
if(node->next[c-'a']==NULL) node->next[c-'a'] = new Trie();
node = node->next[c-'a'];
}
node->isEnd = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie* node = this;
for(auto c: word)
{
node = node->next[c-'a'];
if(node==NULL || !node->isEnd) return false;
}
return node->isEnd;
}
};
class Solution {
public:
string longestWord(vector<string>& words) {
Trie trie;
for (auto word: words) trie.insert(word);
string ans = "";
for (auto word: words)
{
if(trie.search(word))
if (word.size() > ans.size() || (word.size() == ans.size() && word <ans))
ans = word;
}
return ans;
}
};
边栏推荐
- Log4J日志配置详解
- [audio] analysis and summary of PCM audio data transmitted by I2S (I)
- OSI model, tcp/ip model
- [STM32] STM32 SDIO SD card read / write test (II) -- SD_ Power on phase of init
- PAT基础习题集7-26 单词长度(精简代码)
- 【QT源代码复用】QDateTimeEdit的下拉按钮事件响应
- Summary 20220211
- Summary 20220208 (KMP)
- Fortress machine, DMZ area
- LeetCode:626. 换座位
猜你喜欢
数据湖简单记录
01. Open closed principle
App mobile terminal test [6] application program (APK) package management and activity
05. Law of Demeter LOD
App mobile End test [6] application (APK) package Management and Activity
App移動端測試【6】應用程序(apk)包管理與activity
bjyx
[Nordic] nrf52810 OTA upgrade (III) – DFU theoretical analysis
六度空间
Summary 20220121
随机推荐
bjyx
ps: 如何调出辅助线
Leetcode 2039. when the network is idle
[FatFs] FAT32 file system protocol summary (Theory + Practice)
App mobile End test [6] application (APK) package Management and Activity
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so'
04. interface aggregation principle
Leetcode 654. maximum binary tree
PTA 6-11 求自定类型元素序列的中位数 (25 分)
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
PTA搜索树判断
LeetCode: 620. 有趣的电影
QT | modal dialog and modeless dialog qdialog
LeetCode:626. 换座位
Summary 20220119
04.接口隔离原则(Interface Segregation Principle)
03. simple responsibility principle
Six dimensional space
STM32+ESP8266+MQTT协议连接OneNet物联网平台
(c语言)数组是一种特殊的指针?