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

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

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

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

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

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

  4. Longest Word in Dictionary through Deleting - LeetCode

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

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

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

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

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

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

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

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

随机推荐

  1. JZOJ 3508. 【NOIP2013模拟11.5B组】好元素

    3508. [NOIP2013模拟11.5B组]好元素(good) (File IO): input:good.in output:good.out Time Limits: 2000 ms  Mem ...

  2. python爬虫入门八:多进程/多线程

    什么是多线程/多进程 引用虫师的解释: 计算机程序只不过是磁盘中可执行的,二进制(或其它类型)的数据.它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命期. 进程(有时被称为重量级进程)是 ...

  3. request_resource

    1.全局变量 resource结构体定义如下,指针parent.sibling.child用于构建树状结构. struct resource { resource_size_t start; reso ...

  4. 笔记-python-standard library-17.2 multiprocessing

    笔记-python-standard library-17.2 multiprocessing 1.      multiprocessing source code:Lib/multiprocess ...

  5. Hive UDTF开发指南

    在这篇文章中,我们将深入了解用户定义表函数(UDTF),该函数的实现是通过继承org.apache.Hadoop.hive.ql.udf.generic.GenericUDTF这个抽象通用类,UDTF ...

  6. 【Single Number】cpp

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  7. leetcode 【 Pascal's Triangle 】python 实现

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

  8. Aptana Studion出现 duplicate location重复定位报错

    1.下载SVN的时候出现报错 duplicate location 2.点击“available software sites”查看已可用的软件网站 3.在这里可以查看到SVN,勾选SVN复选框,点击 ...

  9. c4d 帮助 prime r16 usage

    c4d   帮助 prime cinema 4d   prime    c4d  基础 前言   usage 开始 双击程序图标   双击一个场景文件   用开始菜单  windows 二选一   从 ...

  10. idea热部署设置(复制)

    提出问题 IntelliJ IDEA工具如何设置热部署??? 解决问题 我的IDEA的版本是:IntelliJ IDEA 14.0.2 第一步:打开tomcat配置 这里写图片描述 第二步: 这里写图 ...