一、题目大意

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. Linux基础学习 | gcc、g++的安装和使用

    安装gcc 1.apt-get命令是debain Linux发新版的APT软件包管理工具. dabian.ubuntu.deepin等Linux系统通过以下命令: 安装gcc:Shell输入sudo ...

  2. 10行 JavaScript 实现文本编辑器

    背景 我们平时用到的浏览器编辑器功能都会比较多,实现的代码逻辑也会非常复杂,往往是作为一个单独插件被引入进来的.但是,现在我只需要一个很基本的内容输入内容编辑的功能,如:粗体.斜体.列表.对齐等.那要 ...

  3. 2022DASCTF X SU 三月春季挑战赛 Calc

    查看代码 #coding=utf-8 from flask import Flask,render_template,url_for,render_template_string,redirect,r ...

  4. Python IDLE清屏

    在学习和使用Python的过程中,少不了要与Python IDLE打交道.但使用 Python IDLE 都会遇到一个常见而又懊恼的问题--要怎么清屏? 答案是为IDLE增加一个清屏的扩展ClearW ...

  5. vue引入swiper

    https://github.com/surmon-china/vue-awesome-swiper/blob/master/examples/03-pagination.vue https://su ...

  6. 什么是机器学习的特征工程?【数据集特征抽取(字典,文本TF-Idf)、特征预处理(标准化,归一化)、特征降维(低方差,相关系数,PCA)】

    2.特征工程 2.1 数据集 2.1.1 可用数据集 Kaggle网址:https://www.kaggle.com/datasets UCI数据集网址: http://archive.ics.uci ...

  7. .NET如何快速比较两个byte数组是否相等

    目录 前言 评测方案 几种不同的方案 For循环 Memcmp 64字长优化 SIMD Sse Avx2 SequenceCompare 总结 参考文献 前言 之前在群里面有群友问过一个这样的问题,在 ...

  8. Servlet学习记录

    个人认为servlet属于一种控制程序,可以处理浏览器的请求并做出对应的回应.我们经常使用的是让一个类去继承HttpServlet,然后在doget或者dopost里面写东西. 目前我个人常在doge ...

  9. SpringBoot-总结

    SpringBoot一站式开发 官网:https://spring.io/projects/spring-boot Spring Boot可以轻松创建独立的.基于Spring的生产级应用程序,它可以让 ...

  10. linux find命令 -mtime参数 根据修改时间查找文件

    命令:find 搜索路径 -mtime n 主要说明n的含义: 例: n=5 "5"指的是前 5~6 天那一天修改的文件 n=-5 "-5"指的是 5 天内修改 ...