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 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.
public class Solution {
public String findLongestWord(String s, List<String> d) {
Comparator<String> comp = new Comparator<String>(){
public int compare(String s1,String s2){
if(s1.length()!=s2.length()){
return s2.length()-s1.length();
}else{
return s1.compareTo(s2);
}
}
};
Collections.sort(d,comp);
for(String str:d){
int i=0;
for(char c:s.toCharArray()){
if(i<str.length()&&str.charAt(i)==c) i++;
}
if(i==str.length()) return str;
}
return "";
}
}
//the run time complexity could be nlongn, the space complexity could be O(1);
public class Solution {
public String findLongestWord(String s, List<String> d) {
String longest = "";
for(String str:d){
int i = 0;
for(char c:s.toCharArray()){
if(i<str.length()&&str.charAt(i)==c) i++;
}
if(i==str.length()&&str.length()>=longest.length()){
if(str.length()>longest.length()||str.compareTo(longest)<0){
longest = str;
}
}
}
return longest;
}
}
//the run time could be O(nkaveragep),space complexity could be O(1);
524. Longest Word in Dictionary through Deleting的更多相关文章
- #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【Medium】【删除后得到的字典中的最长单词】
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 ...
- 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 through Deleting 删除后得到的字典中的最长单词
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- [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——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- Bank Simulation Program银行管理系统C++ :)
设计并实现简单的银行存取款系统,系统主界面包括登录和注册两个选项,选择登录,提示用户输入银行帐号和密码,验证通过后进入主界面,主界面包括:存款.取款.查询余额.历史记录.修改密码等功能.注册功能让用户 ...
- (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中
本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...
- TCP/IP网络编程之多线程服务端的实现(一)
为什么引入线程 为了实现服务端并发处理客户端请求,我们介绍了多进程模型.select和epoll,这三种办法各有优缺点.创建(复制)进程的工作本身会给操作系统带来相当沉重的负担.而且,每个进程有独立的 ...
- c++实验4
1. 车辆基本信息管理 #include <iostream> using namespace std; #include <string> #include "c ...
- 微服务化的不同阶段 Kubernetes 的不同玩法
欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为容器集群管理技术竞争的大赢家,Kubernetes已经和微服务紧密联系,采用Kubernetes的企业往往都开始了微服务架构的探索.然而不同企业 ...
- ios开发学习笔记003-流程控制和类型转换
流程控制 顺序结构.选择结构.循环结构 1.顺序结构 程序默认是顺序执行的. 2.选择结构 if选择语句 第一种情况 if(条件)//条件成立执行下面语句 { //语句 } 第二种情况 if(条件)/ ...
- Python+Selenium练习篇之20-处理Alert弹窗
本文来介绍如何通过Selenium方法去处理网页Alert弹窗,和处理iframe类似,都是通过switch_to方法.这里还是没有找到合适的alert弹窗网站,我们就自己创建一个吧,前面文章介绍了如 ...
- uploadify 报http 302错误
uploadify 报http 302错误 原因是系统采用Forms认证,服务端加入匿名认证即可 具体配置如下: <location path="Base/Base/Upload&qu ...
- [python][django学习篇][6]操作数据库
查询(取)数据 >>> Category.objects.all() <QuerySet [<Category: Category object>]> > ...
- 常用JS整理
目录 1 事件 a addEventListener--绑定事件b removeEventListener--解绑事件,只能解开addEventListener绑定的事件 2 JS获取节点信息a by ...