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的更多相关文章

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

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

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

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

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

  4. leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词

    一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...

  5. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  6. Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

    这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...

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

  8. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. 【python】Leetcode每日一题-删除有序数组中的重复项

    [python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...

随机推荐

  1. 【JAVA】 05-String类和JDK5

    链接: 笔记目录:毕向东Java基础视频教程-笔记 GitHub库:JavaBXD33 目录: <> <> 内容待整理: API-String 特点 String类: 1.St ...

  2. JS中对象数据类型的基本结构和操作

    Object类型 ECMAScript中的队形其实就是一组数据和功能的集合.对象可以通过执行new操作符后跟要创建的对象类型的名称来创建.而创建Object类型的示例并为其添加属性和(或)方法,就可以 ...

  3. tensorflow的reshape操作tf.reshape()

    在处理图像数据的时候总会遇到输入图像的维数不符合的情况,此时tensorflow中reshape()就很好的解决了这个问题. 更为详细的可以参考官方文档说明: numpy.reshape reshap ...

  4. Codeforces Round #392 (Div. 2) - C

    题目链接:http://codeforces.com/contest/758/problem/C 题意:给定N*M矩阵的教室,每个位置都有一个学生,Sergei坐在[X,Y],然后老师会问K个问题,对 ...

  5. shlwapi.h文件夹文件是否存在

    { if( NULL == lpszFileName) { return FALSE; } if (PathFileExists(lpszFileName)) { return TRUE; } els ...

  6. elasticsearch删除

    1.根据id删除 2.根据查询条件删除

  7. 网路编程和并发:2.什么是C/S和B/S架构?

    1.C/S 架构 客户端和服务器之间的架构.Client-Server也称客户机服务器模型. 在Client/Server结构的系统中,应用程序分为客户端和服务器两点部分,客户端为每个用户所有,服务器 ...

  8. 指针使用const修饰总结

    1 double rates[5] = {1, 2, 3, 4, 5}; const double * pd = rates; 被pd指向的值不可改变,比如,不允许*pd = 20 但是pd的指向改变 ...

  9. 零基础python教程-Python解释器是什么?

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  10. C++ 递推法 斐波那契数列 兔子产仔

    #include "stdio.h" #include "iostream" int Fibonacci(int n) { int t1, t2; || n = ...