[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 ...
随机推荐
- robotframework环境搭建问题
启动的时候报错,应该是环境变量没有配置好 错误: command: pybot.bat --argumentfile c:\users\keikei\appdata\local\temp\RIDEam ...
- Struts2学习笔记五 拦截器
拦截器,在AOP中用于在某个方法或字段被访问之前,进行拦截,然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. Struts2中,拦截器是动态拦截Action调用的对象.它提供了一种机制可以使 ...
- Java基础学习笔记二十七 DBUtils和连接池
DBUtils 如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils.DBUtils就是JDBC的简化开发 ...
- Maven安装配置【WIN10】
环境 WIN10 Maven 3.5.3 下载 下载地址:https://maven.apache.org/download.cgi 安装配置 选择好路径后一路 next 默认,安装完成. 环境变量设 ...
- C语言第六周博客作业--数据类型
一.PTA实验作业 题目1: 7-6 掉入陷阱的数字 1. 本题PTA提交列表 2.设计思路 定义变量N,i,g=1表示位数,a表示各位数字相加的和,b=0,j,N1,c,d用于储存N do{ for ...
- alpha-咸鱼冲刺day7
一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 正在写登陆+注册ing 注册搞出来了!!!!!!!!QAQ(喜极而泣!!!!.jpg) 四,问题困难 数据流程大概是搞定了.不过 ...
- Alpha冲刺第一天
Alpha冲刺第一天 站立式会议 项目进展 项目的第一天,主要工作是对项目的开发进行规划,以及将规划的成果转化为燃尽图与博客文章.依据项目需求分析报告与开题报告中已经完成的设计任务和项目规划,我们将系 ...
- Tornado websocket应用
应用场景 WebSocket 的特点如下 适合服务器主动推送的场景(好友上线,即时聊天信息,火灾警告,股票涨停等) 相对于Ajax和Long poll等轮询技术,它更高效,不耗费网络带宽和计算资源 它 ...
- Hibernate之SQL查询
Hibernate支持使用原生的SQL语句进行查询.使用原生的SQL的好处是:可以利用某些数据库的特性(不同的数据库SQL 语法会有所差异), 将原有的使用JDBC作为持久层技术的应用 ,迁移到使用H ...
- Css之导航栏下拉菜单
Css: /*下拉菜单学习-2017.12.17 20:17 added by ldb*/ ul{ list-style-type:none; margin:; padding:; overflow: ...