【LeetCode】524. Longest Word in Dictionary through Deleting 解题记录
问题描述
Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
测试样例
1 | Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"] |
1 | Input: s = "abpcplea", dictionary = ["a","b","c"] |
说明
1 | 1 <= s.length <= 1000 |
解题
思路
采用双指针来求解。
对于 dictionary
中的每一个字符串,将其与 s
进行匹配,若匹配,则判断其长度和字典序是否更优。
补充:
- 双指针
- 时间复杂度
O(n)
代码
1 | import java.util.Collections; |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论