【LeetCode】Longest Word in Dictionary through Deleting 解题报告
【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 解题报告的更多相关文章
- [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 ...
- 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 ...
- Longest Word in Dictionary through Deleting - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- [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 ...
- 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 ...
- 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 ...
- leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
随机推荐
- Docker实用命令介绍
Docker实用命令介绍 1. docker启动.关闭.停止 ╭─wil-xz in ~ 12:15:44 ╰─٩(ŏ﹏ŏ.)۶ service docker restart Redirecting ...
- 听老外吐槽框架设计,Why I Hate Frameworks?
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. Hello,小伙伴们,今天不聊技术,分享点有意思的东西.前段时间,表弟给我发过来一篇老外写的文章,以略带讽刺的对话方式调侃了自己对框架的看法,我 ...
- 日常Java 2021/10/3
方法: 用System.out.println()来解释,println()是一个方法,System是系统类,out 是标准输出对象. 也就是调用系统类中的对象中的方法. 注重方法:可以是程序简洁,有 ...
- accommodate, accompany
accommodate 词源: to make fit, suitable; 近/反义词: adapt, adjust, lodge; disoblige, incommode, misfit Lod ...
- Learning Spark中文版--第五章--加载保存数据(1)
开发工程师和数据科学家都会受益于本章的部分内容.工程师可能希望探索更多的输出格式,看看有没有一些适合他们下游用户的格式.数据科学家可能会更关注他们已经使用的数据格式. Motivation 我 ...
- 大数据学习day19-----spark02-------0 零碎知识点(分区,分区和分区器的区别) 1. RDD的使用(RDD的概念,特点,创建rdd的方式以及常见rdd的算子) 2.Spark中的一些重要概念
0. 零碎概念 (1) 这个有点疑惑,有可能是错误的. (2) 此处就算地址写错了也不会报错,因为此操作只是读取数据的操作(元数据),表示从此地址读取数据但并没有进行读取数据的操作 (3)分区(有时间 ...
- 商业爬虫学习笔记day3
一. 付费代理发送请求的两种方式 第一种方式: (1)代理ip,形式如下: money_proxy = {"http":"username:pwd@192.168.12. ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- UNIX基本命令
### 1. 必学命令 help [子命令] : 查看某一个具体的子命令的使用方法### 2. 常用命令 - cd path : 将当前路径切换到path路径 - pwd : 查看当前所在路径 - l ...
- 【Linux】【Shell】【text】文本处理工具
文本查看及处理工具:wc, cut, sort, uniq, diff, patch wc:word count wc [OPTION]... [FILE]... -l: lines -w:words ...