LeetCode——Longest Word in Dictionary through Deleting
1. Question
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.
2. Solution
直接用输入字符串去匹配字典中的每一个字符串。时间复杂度O(nk),n为字典中字符串的个数,k为输入字符串的长度。
3. Code
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
string longest = "";
for (string str : d) {
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == str[j])
j++;
}
// 字符串匹配完以及长度大于等于
if (j == str.length() && str.length() >= longest.length()) {
// 要么更长,如果一样长,那么就看字典序
if (str.length() > longest.length() || str < longest)
longest = str;
}
}
return longest;
}
};
LeetCode——Longest Word in Dictionary through Deleting的更多相关文章
- [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 ...
- 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.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- [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 ...
- 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 ...
- 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 ...
- leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
随机推荐
- 自定义DataSet
//创建数据集 DataSet dataSet = new DataSet(); //创建虚拟数据表 DataTable datatable = new DataTable(); //获取列集合,添加 ...
- 污染Bootstrap modal 通过 css选择器 避免
w 对框架的掌握.改进. 0-存在重复代码,需要改正,js timepicker框架传入类名: 1-大量的点击块,怎样避免对每个块重复写modal? <style> .w > td ...
- php 乘除法原理
w $wdays = ceil(($wmaxutime-$wminutime)/(24*3600)); $wdays = ceil(($wmaxutime-$wminutime)/243600); 二 ...
- qt and redis desktop manager
(ubuntu desktop)http://blog.csdn.net/ficksong/article/details/7497827 redis manager in ubuntu wget h ...
- jquery.easing的使用
下载地址:http://www.jb51.net/jiaoben/32922.html 基本语法:easing:格式为json,{duration:持续时间,easing:过渡效果,complete: ...
- imToken 测评通关攻略
imToken 测评通关攻略 2017-10-19 imToken 在 1.3.3 版本新增了用户风险测评系统, 目的是为了让更多的用户了解钱包安全知识以及区块链的基本概念, 从某种程度上提升了整个区 ...
- Web项目管理工具精选(上)
原文:Web项目管理工具精选(上) 随着新兴科技公司的蓬勃发展,不少Web应用和浏览器工具在开发者.设计者.自由职业者和项目经理中间流行开来.这些工具在不断发展,我们也看到越来越多的桌面应用.移动应用 ...
- 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
本文背景: 在编程中,很多Windows或C++的内存函数不知道有什么区别,更别谈有效使用:根本的原因是,没有清楚的理解操作系统的内存管理机制,本文企图通过简单的总结描述,结合实例来阐明这个机制. 本 ...
- Incorrect key file for table ' '; try to repair it
场景:为有150W的数据表增加字段时,报错 解决:在my.ini配置临时目录configure tmpdir. Where MySQL Stores Temporary Files
- oracle入门(6)——PL/SQL常用语法
[本文介绍] 本文不是”语法大全“,只是记录下作项目里自己常用的一些语法.方便查询. [语法] [输出] (1)输出语法 DBMS_OUTPUT.PUT_LINE( ) [定义] (1)定义变 ...