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

题目

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

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

思路:双指针

代码

java 复制代码
class Solution {
    public String findLongestWord(String s, List<String> dictionary) {
        String res = "";
        for (String t : dictionary) {
            int i = 0;
            int j = 0;
            while (i < t.length() && j < s.length()) {
                if (t.charAt(i) == s.charAt(j)) {
                    i++;
                }
                j++;
            }
            if (i == t.length()) {
                if (t.length() > res.length() || t.length() == res.length() && t.compareTo(res) < 0) {
                    res = t;
                }
            }
        }
        return res;
    }
}

性能

相关推荐
hn小菜鸡2 小时前
LeetCode 2570.合并两个二维数组-求和法
数据结构·算法·leetcode
Greedy Alg2 小时前
LeetCode 226. 翻转二叉树
算法
我要成为c嘎嘎大王2 小时前
【C++】模版专题
c++·算法
jndingxin2 小时前
算法面试(5)------NMS(非极大值抑制)原理 Soft-NMS、DIoU-NMS 是什么?
人工智能·算法·目标跟踪
苏纪云3 小时前
算法<java>——排序(冒泡、插入、选择、归并、快速、计数、堆、桶、基数)
java·开发语言·算法
滋滋不吱吱3 小时前
栈的进阶篇
数据结构·算法·leetcode
YQ_ZJH3 小时前
Java List列表创建方法大总结
java·开发语言·数据结构·算法·list
Absinthe_苦艾酒3 小时前
golang基础语法(五)切片
开发语言·算法·golang
Rsingstarzengjx4 小时前
【算法】【数学】【质数】质数相关 leetcode 204
数据结构·算法