题目
来源:力扣(LeetCode)
链接: https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/
给你一个字符串 s 和一个字符串数组 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
本题讨论如何从给定字符串s中通过删除一些字符,使其变成字典dictionary中最长且字典序最小的单词。通过优化的遍历策略和排序技巧,找到符合条件的最长单词,时间复杂度为O(d×m×logd+d×(m+n)),空间复杂度为O(d×m)。
订阅专栏 解锁全文
491

被折叠的 条评论
为什么被折叠?



