一、题目大意

https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting

给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。

如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。

示例 1:

输入:s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]

输出:"apple"

示例 2:

输入:s = "abpcplea", dictionary = ["a","b","c"]

输出:"a"

提示:

  • 1 <= s.length <= 1000
  • 1 <= dictionary.length <= 1000
  • 1 <= dictionary[i].length <= 1000
  • s 和 dictionary[i] 仅由小写英文字母组成

二、解题思路

题意:1、字典中单词的每个字母都在字符串中按顺序出现 2、找出长度最长且字母序最小

思路:1、实现判断一个单词中的所有字符按顺序出现在另一个字符串中 2、多个相同长度的字符串取字母序最小的串

三、解题方法

3.1 Java实现

public class Solution {
public String findLongestWord(String s, List<String> dictionary) {
List<String> res = new ArrayList<>();
int maxLen = 0;
for (String tmp : dictionary) {
if (isSubstring(s, tmp)) {
if (tmp.length() > maxLen) {
maxLen = tmp.length();
res.clear();
res.add(tmp);
} else if (tmp.length() == maxLen) {
res.add(tmp);
}
}
}
return getMin(res);
} private boolean isSubstring(String str, String subStr) {
int i = str.length() - 1;
int j = subStr.length() - 1;
while (i >= 0 && j >= 0) {
if (str.charAt(i) == subStr.charAt(j)) {
j--;
}
i--;
}
return j == -1;
} private String getMin(List<String> list) {
if (list == null || list.size() == 0) {
return "";
}
String minStr = list.get(0);
for (String tmp : list) {
if (minStr.compareTo(tmp) > 0) {
minStr = tmp;
}
}
return minStr;
}
}

四、总结小记

  • 2022/5/20 把解题思路理清,一切就顺理成章了

leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词的更多相关文章

  1. Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)

    524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...

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

  3. leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java

    1. 具体题目 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空 ...

  4. 【LeetCode】524-通过删除字母匹配到字典里最长单词

    题目描述 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空字符串 ...

  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. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

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

  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. Longest Word in Dictionary through Deleting - LeetCode

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

随机推荐

  1. iOS全埋点解决方案-界面预览事件

    前言 ​ 我们先了解 UIViewController 生命周期相关的内容和 iOS 的"黑魔法" Method Swizzling.然后再了解页面浏览事件($AppViewScr ...

  2. Canvas 核心技术

    最近项目需求中要写较多H5小游戏,游戏本身体量不是很复杂,主要是承载较多业务逻辑,所以决定用canvas来完成游戏部分.之前只是知道H5中有canvas这个东西,也知道它大概是画图的,但具体怎么用,还 ...

  3. python-班级人员信息统计

    输入a,b班的名单,并进行如下统计. 输入格式: 第1行::a班名单,一串字符串,每个字符代表一个学生,无空格,可能有重复字符.第2行::b班名单,一串字符串,每个学生名称以1个或多个空格分隔,可能有 ...

  4. javaweb之删除功能

    对数据库的删除,主要是通过表中的一个数据查询来进行逐个删除,否则会清空整张表. 一.dao层 在dao层加入删除方法 public boolean delete(Course n) { boolean ...

  5. SpringMVC 配置和请求方式

    SpringMVC 总结内容 一.什么是 Spring MVC ? Spring MVC 是 Spring 对 MVC 思想的实现(三层架构) 优点: 二.前端控制器 Spring MVC 中的前端控 ...

  6. macos停止MySQL服务

    1.命令行中 使用 find /usr  -name mysql 查找自己电脑中MySQL的安装位置 例如我查找到我电脑MySQL安装位置是 /usr/local/Cellar/mysql@5.6/5 ...

  7. 使用vue-cli构建工具构建vue项目时候组件的使用

    <template> <div class="contains"> <!-- <div class="main"> & ...

  8. python 反序列化

    Python-反序列化函数使用 pickle.dump(obj, file) : 将对象序列化后保存到文件 pickle.load(file) : 读取文件, 将文件中的序列化内容反序列化为对象 pi ...

  9. Mybatis模糊查询结果为空的解决方案

    写在前面 Mybatis使用模糊查询,查询结果为空的解决方案,我的代码是 select * from sp_user where 1=1 <if test="username!=nul ...

  10. Java语言学习day07--7月6日

    今日内容介绍1.流程控制语句switch2.数组 ###01switch语句解构​ * A:switch语句解构 * a:switch只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码. * ...