【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 ...
随机推荐
- MySQL-数据库多表关联查询太慢,如何进行SQL语句优化
工作中我们经常用到多个left join去关联其他表查询结果,但是随着数据量的增加,一个表的数据达到百万级别后,这种普通的left join查询将非常的耗时. 举个例子: 现在porder表有 10 ...
- 55. Binary Tree Preorder Traversal
Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 119655 Tota ...
- HTML三层界面显示
1.效果示意图 2.主要标签属性 3.实现代码 1.效果示意图 要实现类似如下效果:点击"大模态框",中间出现一层遮盖整个页面的半透明页面,最上面出现"Large mod ...
- 也谈string.Join和StringBuilder的性能比较
前几天在园子里面看到一篇讲StringBuilder性能的文章.文章里面给出了一个测试用例,比较StringBuilder.AppendJoin和String.Join的性能.根据该测试结果,&quo ...
- 数仓day02
1. 什么是ETL,ETL都是怎么实现的? ETL中文全称为:抽取.转换.加载 extract transform load ETL是传数仓开发中的一个重要环节.它指的是,ETL负责将分布的. ...
- oracle(创建数据库对象)
1 --创建数据库 2 --1.SYSDBA系统权限 3 startup:--启动数据库. 4 shutdown:--关闭数据库. 5 alter database[mount]|[open]|[ba ...
- IOS_UIButton去掉系统的按下高亮置灰效果
setAdjustsImageWhenHighlighted // default is YES. if YES, image is drawn darker when highlighted(p ...
- ActiveRecord教程
(一.ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数 ...
- maven 是什么?
在了解maven的概念之前,我一直都在项目中使用maven,但是对于maven的了解,只能说连个皮毛都算不上,一直到项目中,自己机械化的deploy项目的时候,发现报错,赶紧报告开发组长,这私服是不是 ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(9. 安装consul实现服务注册发现)
1. 简介 1.1. 官方网站: https://www.consul.io 1.2. Consul的功能: 服务发现:通过DNS或HTTP接口使得消费者发现服务,应用程序可以轻松找到所依赖的服务. ...