leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一、题目大意
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 通过删除字母匹配到字典里最长单词的更多相关文章
- Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)
524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...
- [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 ...
- leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java
1. 具体题目 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空 ...
- 【LeetCode】524-通过删除字母匹配到字典里最长单词
题目描述 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空字符串 ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 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 ...
- Longest Word in Dictionary through Deleting - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...
随机推荐
- 外部晶振的使用原因与内部RC振荡器的使用方法
原因一 早些年,芯片的生产制作工艺也许还不能够将晶振做进芯片内部,但是现在可以了.这个问题主要还是实用性和成本决定的. 原因二 芯片和晶振的材料是不同的,芯片 (集成电路) 的材料是硅,而晶体则是 ...
- MySQL索引机制(详细+原理+解析)
MySQL索引机制 永远年轻,永远热泪盈眶 一.索引的类型与常见的操作 前缀索引 MySQL 前缀索引能有效减小索引文件的大小,提高索引的速度.但是前缀索引也有它的坏处:MySQL 不能在 ORDER ...
- dll反编译(修改引用文件、修改代码)再生成dll
问题描述 我们在日常开发中经常会遇到,想要对dll文件做修改的操作,但苦于没有源代码,只能想想其他办法 解决问题 办法就是通过几个工具来反编译.正向编译.修改属性 反编译.正编译 参考https:// ...
- React中Ref 的使用 React-踩坑记_05
React中Ref 的使用 React v16.6.3 在典型的React数据流中,props是父组件与其子组件交互的唯一方式.要修改子项,请使用new props 重新呈现它.但是,在某些情况下,需 ...
- 【Android开发】【布局】 仿QQ的UI
Demo地址
- 【Android Studio】Gradle统一管理版本号引用配置
1.在根目录下的build.gradle文件下添加 ext{ .... } 中的内容 ...... // Define versions in a single place ext { // SDK ...
- java中public和缺省这两个访问权限的根本区别?
为了区分开public和缺省的区别,我们要引进包(package)的概念.包就像咱们磁盘上的目录一样,马克-to-win.package a;就是定义说当前的目录为a.底下编的任何的类,都会出现在当前 ...
- Mybatis实现批量删除数据
Mybatis实现批量删除操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...
- Ubuntu安装docker-compose(摘自官网,自用)
安装 Docker Compose 预计阅读时间:8分钟 加速 Docker 桌面中的新功能 Docker Desktop 可帮助您在 Mac 和 Windows 上轻松构建.共享和运行容器,就像在 ...
- 9. Lab: file system
https://pdos.csail.mit.edu/6.S081/2021/labs/fs.html 1. Large files (moderate) 1.1 要求 Modify bmap() s ...