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 ...
随机推荐
- Python9-MySQL索引-外键-day43
1.以ATM引出DBMS2.MySQL -服务端 -客户端3.通信交流 -授权 -SQL语句 -数据库 create database db1 default charset=utf8; drop d ...
- 使用 CAST
使用 CAST: CAST ( expression AS data_type ) 使用 CONVERT: CONVERT (data_type[(length)], expression [, st ...
- libevent 信号事件实现方式
学会使用libevent,才能真正的掌握其是实现原理,我们先从一个简短的测试用例开始: #include <sys/types.h> #include <sys/stat.h> ...
- ACM模板
#include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...
- SpringMVC controller接收的中文参数乱码
CharacterEncodingFilter只对POST请求有用,GET请求的需要对你运行的tomcat 目录conf/server.xml文件中<Connector connectionTi ...
- vue时时监听input输入框中 输入内容 写法
Vue input 监听 使用 v-on:input="change" 实现即可 App.vue <template> <div> <md-field ...
- Redis实现之数据库(三)
过期键删除策略 在Redis实现之数据库(二)一小节中,我们知道了数据库键的过期时间都保存在过期字典中,又知道了如果根据过期时间去判断一个键是否过期,现在剩下的问题是:如果一个键过期了,那么它什么时候 ...
- 43、gridview或者listview的adapter优化
1.在getview时,如果是一个textview,那么不用每次都new一个或者inflater直接返回,可以先判断convertview是否为空,如果为空则new或者inflate,否则直接返回co ...
- 【Copy List with Random Pointer】cpp
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- leetcode 【 Subsets 】python 实现
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...