leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java
1. 具体题目
给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到。如果答案不止一个,返回长度最长且字典顺序最小的字符串。如果答案不存在,则返回空字符串。
示例 1: 输入: s = "abpcplea", d = ["ale","apple","monkey","plea"] 输出: "apple"
2. 思路分析
遍历字符串数组,将其中每个长度大于当前结果值 ans or 长度相同但字典顺序小于 ans 的字符串 str 与 s 比较,若 str 是 s 的子串则更新结果值。而比较过程中可能会出现:s 已经遍历完,而当前 str 还未遍历完,此类情况不应更新结果值。
3.代码
public String findLongestWord(String s, List<String> d) {
String ans = "";
for(String str : d){
if(str.length() < ans.length() || str.length() == ans.length() && ans.compareTo(str) < 0) continue;
int i = 0, j = 0;
while(i < s.length() && j < str.length()){
if(s.charAt(i) == str.charAt(j)){
j++;
}
i++;
}
if(j == str.length())
ans = str;
}
return ans;
}
leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java的更多相关文章
- Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)
524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...
- 【LeetCode】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. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
这道题相似 Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...
- [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] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 【python】Leetcode每日一题-删除有序数组中的重复项
[python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...
随机推荐
- 分布式ID增强篇--优化时钟回拨问题
原生实现 本文承接sharding-jdbc源码之分布式ID,在这篇文章中详细介绍了sharding-jdbc的分布式ID是如何实现的:很遗憾的是sharding-jdbc只是基于snowflake算 ...
- Source Insight symbol not found
使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...
- C++ STL常见数据结构(容器)分类
vector:(连续的空间存储,可以使用[]操作符)快速的访问随机的元素,快速的在末尾插入元素,但是在序列中间的插入,删除元素要慢,而且如果一开始分配的空间不够的话,可能重新分配更大空间,拷贝的性能开 ...
- iOS crash log 解析
iOS开发中,经常遇到App在开发及测试时不会有问题,但是装在别人的设备中会出现各种不定时的莫名的 crash,因为iOS设备会保存应用的大部分的 crash Log,所以可以通过 crash Log ...
- MySQL04-- 版本区别及管理
目录 MySQL版本区别及管理 一.MySQL5.6与MySQL5.7安装的区别 二.MySQL用户权限管理 三.MySQL连接管理 四.MySQL启动关闭流程 五.MySQL实例初始化配置 六.My ...
- AES apache commons-crypto 对称加密
apache实现的AES256加密 官方用户指导链接:http://commons.apache.org/proper/commons-crypto/userguide.html 官方字节缓存实现的例 ...
- 用Matlab的.m脚本文件处理实验室数据
找到相应的文件 findfile %1 打开文件夹 %2 拷贝第一个文件 %3 关闭当前文件,再次拷贝新的文件,直到文件末尾结束 clc clear DST_PATH_t = 'C:\Users\Ma ...
- No enclosing instance of type TestGson is accessible. Must qualify the allocation with an enclosing instance of type TestGson (e.g. x.new A() where x is an instance of TestGson).
main方法中实例化内部类报错: public class TestGson { public static void main(String[] args) { Gson gson=new Gson ...
- Linux 给用户 赋某个文件夹操作的权限
https://my.oschina.net/cqyj/blog/1796047 在root用户登录的情况,赋予opt目录给liuhai这个用户权限 示例代码: # 将目录/opt 及其下面的所有文件 ...
- Linux重定向命令(stdout, stdin, stderr)
ls -l /usr/bin > ls-output.txt 将输出结果重定向到 ls-output.txt 文件.注意:再次使用> ls-output.txt会默认覆盖源文件.如果要追加 ...