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:

  1. All the strings in the input will only contain lower-case letters.
  2. The size of the dictionary won't exceed 1,000.
  3. 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 "";
}
};

  天气刚好

最新文章

  1. Yii2的深入学习--自动加载机制
  2. mindmup-editabletable-编辑table的使用
  3. Halcon学习笔记之缺陷检测(一)
  4. Chp3: Stacks and Queue
  5. Umbraco中Document Type取名限制
  6. ntopng-一款流量审计框架的安装以及应用
  7. 【Beta阶段】第五次scrum meeting
  8. Marriage is Stable
  9. .net core控制台应用程序初识
  10. Spring学习(一):理解IoC容器
  11. [Swift]Xcode标记:MARK、TODO、FIXME
  12. 洛谷P3235 [HNOI2014]江南乐(Multi-SG)
  13. 前端性能优化成神之路--SSR(服务端渲染)
  14. AWS事故总结,几招教你规避风险
  15. February 8th, 2018 Week 6th Thursday
  16. ROC,AUC,Precision,Recall,F1的介绍与计算
  17. JFreeChart 之折线图
  18. Material Designer的低版本兼容实现(六)—— Ripple Layout
  19. [转] libtool的作用及应用
  20. 自定义样式,使用浏览器阅读epub格式的电子书

热门文章

  1. xss挑战之旅wp
  2. 【算法】LeetCode算法题-Roman To Integer
  3. CSS鼠标悬浮DIV后显示DIV外的按钮
  4. Unittest框架小结
  5. 如何学习Linux性能优化?
  6. 数以亿计运行PHP的网站即将面临严重的安全风险
  7. chrome-performance页面性能分析使用教程
  8. 转://Oracle 事务探索与实例(一)
  9. 9.1 oop习题集合
  10. pip 安装指定版本软件包