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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

编辑距离(超时)

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Solution {

    public boolean edit(String str1, String str2) {
        if (str1.length() < str2.length()) {
            return false;
        }

        boolean[][] dp = new boolean[str1.length() + 1][str2.length() + 1];

        for (int i = 0; i <= str1.length(); ++i) {
            dp[i][0] = true;
        }

        for (int i = 1; i <= str1.length(); ++i) {
            for (int j = 1; j <= Math.min(i, str2.length()); ++j) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        return dp[str1.length()][str2.length()];
    }


    public String findLongestWord(String s, List<String> dictionary) {
        if (s == null || s.length() == 0 || dictionary == null || dictionary.size() == 0) {
            return "";
        }
        Collections.sort(dictionary, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() == o2.length()) {
                    return o1.compareTo(o2);
                }
                return Integer.compare(o2.length(), o1.length());
            }
        });
        for (String str : dictionary) {
            if (edit(s, str)) {
                return str;
            }
        }
        return "";
    }
}

双指针

import java.util.List;

class Solution {

    public boolean edit(String str1, String str2) {
        if (str1.length() < str2.length()) {
            return false;
        }

        int p1 = str1.length() - 1, p2 = str2.length() - 1;

        while (p1 >= 0 && p2 >= 0) {
            if (str1.charAt(p1) == str2.charAt(p2)) {
                p1--;
                p2--;
            } else {
                p1--;
            }
        }

        return p2 == -1;
    }


    public String findLongestWord(String s, List<String> dictionary) {
        if (s == null || s.length() == 0 || dictionary == null || dictionary.size() == 0) {
            return "";
        }
        String ans = "";
        for (String str : dictionary) {
            if (edit(s, str)) {
                if (str.length() > ans.length() || (str.length() == ans.length() && str.compareTo(ans) < 0)) {
                    ans = str;
                }
            }
        }
        return ans;
    }
}
posted @   Tianyiya  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示