#Leetcode# 524. Longest Word in Dictionary through Deleting
2024-08-24 16:56:27
https://leetcode.com/problems/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.
代码:
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();
});
int ls = s.length();
for(int i = 0; i < d.size(); i ++) {
int cnt = 0;
for(int j = 0; j < s.size(); j ++) {
if(cnt < d[i].size() && s[j] == d[i][cnt]) ++ cnt;
}
if(cnt == d[i].size()) return d[i];
}
return "";
}
};
天气刚好
最新文章
- Yii2的深入学习--自动加载机制
- mindmup-editabletable-编辑table的使用
- Halcon学习笔记之缺陷检测(一)
- Chp3: Stacks and Queue
- Umbraco中Document Type取名限制
- ntopng-一款流量审计框架的安装以及应用
- 【Beta阶段】第五次scrum meeting
- Marriage is Stable
- .net core控制台应用程序初识
- Spring学习(一):理解IoC容器
- [Swift]Xcode标记:MARK、TODO、FIXME
- 洛谷P3235 [HNOI2014]江南乐(Multi-SG)
- 前端性能优化成神之路--SSR(服务端渲染)
- AWS事故总结,几招教你规避风险
- February 8th, 2018 Week 6th Thursday
- ROC,AUC,Precision,Recall,F1的介绍与计算
- JFreeChart 之折线图
- Material Designer的低版本兼容实现(六)—— Ripple Layout
- [转] libtool的作用及应用
- 自定义样式,使用浏览器阅读epub格式的电子书