[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"] Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"] Output:
"a"
Note:
- All the strings in the input will only contain lower-case letters.
- The size of the dictionary won't exceed 1,000.
- The length of all the strings in the input won't exceed 1,000.
这道题给了我们一个字符串,和一个字典,让我们找到字典中最长的一个单词,这个单词可以通过给定单词通过删除某些字符得到。由于只能删除某些字符,并不能重新排序,所以我们不能通过统计字符出现个数的方法来判断是否能得到该单词,而是只能老老实实的按顺序遍历每一个字符。我们可以给字典排序,通过重写comparator来实现按长度由大到小来排,如果长度相等的就按字母顺序来排。然后我们开始遍历每一个单词,用一个变量i来记录单词中的某个字母的位置,我们遍历给定字符串,如果遍历到单词中的某个字母来,i自增1,如果没有,就继续往下遍历。这样如果最后i和单词长度相等,说明单词中的所有字母都按顺序出现在了字符串s中,由于字典中的单词已经按要求排过序了,所以第一个通过验证的单词一定是正确答案,我们直接返回当前单词即可,参见代码如下:
解法一:
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
sort(d.begin(), d.end(), [](string a, string b){
if (a.size() == b.size()) return a < b;
return a.size() > b.size();
});
for (string str : d) {
int i = ;
for (char c : s) {
if (i < str.size() && c == str[i]) ++i;
}
if (i == str.size()) return str;
}
return "";
}
};
上面的方法中用到了sort排序,对于数组中单词的数量不是很多,但是长度特别长的情况很适用,但是如果对于单词数量特别多,但是每个都不长的话,排序的O(nlgn)时间复杂度可能就不是最好的方法了,也可以不用排序来做。我们遍历字典中的单词,然后还是跟上面的方法一样来验证当前单词是否能由字符串s通过删除字符来得到,如果能得到,而且单词长度大于等于结果res的长度,我们再看是否需要更新结果res,有两种情况是必须要更新结果res的,一个是当前单词长度大于结果res的长度,另一种是当前单词长度和res相同,但是字母顺序小于结果res,这两种情况下更新结果res即可,参见代码如下:
解法二:
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
string res = "";
for (string str : d) {
int i = ;
for (char c : s) {
if (i < str.size() && c == str[i]) ++i;
}
if (i == str.size() && str.size() >= res.size()) {
if (str.size() > res.size() || str < res) {
res = str;
}
}
}
return res;
}
};
参考资料:
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/description/
https://discuss.leetcode.com/topic/80876/10-lines-solutions-for-c
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词的更多相关文章
- 524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- LeetCode——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- Longest Word in Dictionary through Deleting - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
- [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- 524. Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
随机推荐
- [css 揭秘]:CSS揭秘 技巧(一):半透明边框
我的github地址:https://github.com/FannieGirl/ifannie/ 源码都在上面哦 喜欢的给我一个星吧 半透明边框 css 中的半透明颜色,比如用 rgba() 和 h ...
- [poj3461]Oulipo_KMP
Oulipo poj-3461 题目大意:给你两个字符串s和p,问s中有多少个等于p的子串. 注释:$1\le strlen(p)\le 10^4\qquad1\le strlen(s)\le 10^ ...
- RDD概念、特性、缓存策略与容错
一.RDD概念与特性 1. RDD的概念 RDD(Resilient Distributed Dataset),是指弹性分布式数据集.数据集:Spark中的编程是基于RDD的,将原始数据加载到内存变成 ...
- Beta No.7
今天遇到的困难: 构造新适配器的时候出现了某些崩溃的问题 ListView监听器有部分的Bug 今天完成的任务: 陈甘霖:完成相机调用和图库功能,完成阿尔法项目遗留下来的位置调用问题,实现百度定位 蔡 ...
- Build to win--来自小黄衫
写在前面 首先非常荣幸.非常侥幸能以微弱的优势得到这次小黄衫,感谢各位老师同学的帮助,也谢谢来自<构建之法>团队的小黄衫赞助! 这次能够获得小黄衫,就像汪老师上课说的那样,其实,是一个积累 ...
- 项目Alpha冲刺Day12
一.会议照片 二.项目进展 1.今日安排 修复全局的日期转换问题,完成用户所有相关的模块,对全局的异常处理做优化.其他模块进行一部分实现. 2.问题困难 全局异常处理后发现没有进行按照链进行下去,造成 ...
- C语言——第二次作业(2)
作业要求一 PTA作业的提交列表 作业要求二 题目1.删除字符串中数字字符(函数题) 1.设计思路 - (1)算法 第一步:调用定义的函数. 第二步:定义i=0.j=0,i为原字符数组角标,j为删除后 ...
- jwt验证登录信息
为什么要告别session?有这样一个场景,系统的数据量达到千万级,需要几台服务器部署,当一个用户在其中一台服务器登录后,用session保存其登录信息,其他服务器怎么知道该用户登录了?(单点登录), ...
- 前端面试之angular JS
1. angular的数据绑定采用什么机制?详述原理 angularjs的双向数据绑定,采用脏检查(dirty-checking)机制.ng只有在指定事件触发后,才进入 $digest cycle : ...
- nyoj 寻找最大数
寻找最大数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...