【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. 07 MySQL安装图解--Windows版本

    MySQL安装图解 使用微信扫码关注微信公众号,并回复:"MySQL环境",免费获取下载链接! 1.安装MySQL 2.校验MySQL 3.登录MySQL 登录MySQL:mysq ...

  2. c#表中信息点击跳转

    OnRowCommand="gridInfoData_RowCommand" <Columns> <asp:ButtonField HeaderText=&quo ...

  3. javaSE高级篇4 — 反射机制( 含类加载器 ) — 更新完毕

    反射机制 1.反射机制是什么?----英文单词是:reflect.在java.lang包下---这才是java最牛逼的技术 首先提前知道一句话----在java中,有了对象,于是有了类,那么有了类之后 ...

  4. javaSE高级篇6 — 注解( 附:注解底层解析 ) —— 更新完毕

    注解 ---- 英文:annotation 1.注解长什么样子? @xxxxxxx( 一些信息 ) ----- 这个信息可有可无 2.注解可以放在什么地方? 类本身的上面.属性的上面.方法的上面.参数 ...

  5. java生成cron表达式

    bean类: package com.cst.klocwork.service.cron; public class TaskScheduleModel { /** * 所选作业类型: * 1 -&g ...

  6. Linux基础命令---nfsstat显示nfs信息

    nfsstat nfsstat指令用来显示nfs客户端和服务器的活动信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法       nfsstat  ...

  7. mysql之对象创建

    1 --创建表空间 2 create tablespace tablespace_name 3 innodb and ndb: 4 add datafile 'file_name' 5 innodb ...

  8. Reactor之发射器(Flux、Mono)转换操作函数

    数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...

  9. 【C/C++】最长无重复子数组

    题目描述 给定一个数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同. 子数组是连续的,比如[1,2,3,4,5]的子数组有[1,2],[2,3,4]等等,但是[1,3, ...

  10. python实现skywalking邮件告警webhook接口

    1.介绍 Skywalking可以对链路追踪到数据进行告警规则配置,例如响应时间.响应百分比等.发送警告通过调用webhook接口完成.webhook接口用户可以自定义. 2.默认告警规则 告警配置文 ...