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的更多相关文章

  1. [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 ...

  2. Longest Word in Dictionary through Deleting - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...

  3. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  5. #Leetcode# 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词

    一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...

随机推荐

  1. JavaScript学习(5)-Image对象和动态HTML

    JavaScript学习5 1.image 对象 对象引用 document.images[n] document.images["imageName"] document.ima ...

  2. mysql 下数据库升级脚本的编写

    1 升级时必须得存储过程 /**/ drop procedure if exists pro_upgrade; DELIMITER // CREATE DEFINER=`root`@`%` PROCE ...

  3. (转载)移动Web开发技巧汇总

    META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...

  4. UVA11426 GCD - Extreme (II)---欧拉函数的运用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. ObjectDetection中的一些名词中英文对照

    mAP:mean Average Precision,平均精确度 recall rate:召回率 Loss Function Anchro

  6. mongoDb,下载及启动

     mongoDb下载 https://www.mongodb.com/download-center 可视化工具Robomongo下载 https://robomongo.org/download m ...

  7. TP自适应

    最近又要求职了,梳理了下这两年折腾的东西,发现有个产品很可惜,都开发完了,但是没上市.中兴的一款手表,我很喜欢那个金属壳子,结实,拿在手里沉甸甸,可以用来砸核桃. 当时调TP的时候,换了几个厂家,程序 ...

  8. 用tophat和cufflinks分析RNAseq数据[转载]

    转自:http://blog.sciencenet.cn/home.php?mod=space&uid=635619&do=blog&id=884213 //今天看到一篇非常好 ...

  9. Linux服务器access_log日志分析及配置详解(二)

    默认nginx / Linux日志在哪个文件夹? 一般在 xxx.xxx.xxxx.com/home/admin 路径下面的error.log文件和access.log文件error_log logs ...

  10. PKU 1226 Substrings(字符串匹配+暴搜KMP模板)

    原题大意:原题链接 给出n个字符串,找出一个最长的串s,使s或者s的反转字符串(只要其中一个符合就行)同时满足是这n个串的子串. 对于样例,第一组ABCD   BCDFF  BRCD最长的串就是CD; ...