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

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/#/description

题目描述:

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

Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"] Output:
"apple" Input:
s = "abpcplea", d = ["a","b","c"] Output:
"a"

Ways

题目意思是要找出给出的输入字符串去除某些字符后能拼接成的在字典中的最长字典字符串。
如果按照题目给的意思,通过删除字符来判断剩下的字符串是否在字典里无疑是个非常繁重的工作。这个题应该反过来想:对于字典中的每个字典单词串,判断能否由输入字符串中的某些字符元素组成。方法分为两步:1.对字典字符串排序;2.查找到能最优先被输入字符串匹配的字典字符串。
题目中对结果有以下要求:返回最长或长度一样时返回字母表中最前的。那么可以对字典中的字符串按照这两个要求排序:长度降序、长度相同时字母表升序。这样遍历字典字符串列表,第一个能被输入字符串去掉某些字符表示出的字典字符串即为所求。
如何判断字典字符串能被输入字符串去除某些元素后表示出来?方法是对输入字符串的每个字符从左到右遍历,如果输入字符串的某位和字典字符串的第i位相同,那么字典字符串的指针i++指向下一位字符,再判断输入字符串此后是否存在和字典字符串当前位相同的字符,以此类推。
循环结束的i是字典字符串和输入字符串匹配的字符个数,如果i等于字典字符串的长度说明已经输入字符串能通过去掉若干字符的方式表示出字典字符串,循环结束,返回此字典字符串。

public static String findLongestWord(String s, List<String> d) {
Collections.sort(d, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o1.length() != o2.length()) ?
(o2.length() - o1.length()) : o1.compareTo(o2);
}
});
for (String dictWord : d) {
int i = 0;
for (char c : s.toCharArray()) {
if (i < dictWord.length() && c == dictWord.charAt(i)) {
i++;
}
}
if (i == dictWord.length()) {
return dictWord;
}
}
return "";

Date

2017 年 4 月 7 日

【LeetCode】Longest Word in Dictionary through Deleting 解题报告的更多相关文章

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

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

  3. Longest Word in Dictionary through Deleting - LeetCode

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python20判断变量是否存在

    python中检测某个变量是否有定义 第一种方法使用内置函数locals(): locals():获取已定义对象字典 'testvar' in locals().keys() 第二种方法使用内置函数d ...

  2. Linux—yum安装python-pip

    centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...

  3. vector去重--unique

    具体实现见中间源码 function template <algorithm> std::unique equality (1) template <class ForwardIte ...

  4. Flink 实践教程-进阶(2):复杂格式数据抽取

    作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接.亚 ...

  5. 简述安霸pipeline及其关键参数--raw域模块

    何为pipeline: sensor输出是一种叫Bayer 格式的RAW数据图像.ISP 对RAW数据图像的处理流程就是我们说的ISP PipeLine.通过PipeLine的处理,我们可以从一副RA ...

  6. PowerToys插件扩展(类似Alfred)

    在mac系统除了自带的Spotlight还有一个很好用的工具叫Alfred image 在windows系统也有一个很好用的工具叫PowerToys,是微软的一个开源项目 image https:// ...

  7. RocketMQ这样做,压测后性能提高30%

    从官方这边获悉,RocketMQ在4.9.1版本中对消息发送进行了大量的优化,性能提升十分显著,接下来请跟着我一起来欣赏大神们的杰作. 根据RocketMQ4.9.1的更新日志,我们从中提取到关于消息 ...

  8. 日常Java 2021/10/20

    Java提供了一套实现Collection接口的标准集合类 bstractCollection 实现了大部分的集合接口. AbstractList 继承于AbstractCollection并且实现了 ...

  9. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  10. flink-----实时项目---day05-------1. ProcessFunction 2. apply对窗口进行全量聚合 3使用aggregate方法实现增量聚合 4.使用ProcessFunction结合定时器实现排序

    1. ProcessFunction ProcessFunction是一个低级的流处理操作,可以访问所有(非循环)流应用程序的基本构建块: event(流元素) state(容错,一致性,只能在Key ...