Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 524. Longest Word in Dictionary through Deleting #524

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 524. Longest Word in Dictionary through Deleting #524

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

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.

Example 1:

Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
Output: "apple"

Example 2:

Input: s = "abpcplea", dictionary = ["a","b","c"]
Output: "a"

Constraints:

  • 1 <= s.length <= 1000
  • 1 <= dictionary.length <= 1000
  • 1 <= dictionary[i].length <= 1000
  • s and dictionary[i] consist of lowercase English letters.

这道题给了一个字符串,和一个字典,让找到字典中最长的一个单词,这个单词可以通过给定单词通过删除某些字符得到。由于只能删除某些字符,并不能重新排序,所以不能通过统计字符出现个数的方法来判断是否能得到该单词,而是只能老老实实的按顺序遍历每一个字符。这里可以给字典排序,通过重写 comparator 来实现按长度由大到小来排,如果长度相等的就按字母顺序来排。然后开始遍历每一个单词,用一个变量i来记录单词中的某个字母的位置,遍历给定字符串,如果遍历到单词中的某个字母来,i自增1,如果没有,就继续往下遍历。这样如果最后i和单词长度相等,说明单词中的所有字母都按顺序出现在了字符串s中,由于字典中的单词已经按要求排过序了,所以第一个通过验证的单词一定是正确答案,直接返回当前单词即可,参见代码如下:

解法一:

class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) {
        sort(dictionary.begin(), dictionary.end(), [](string a, string b){
            if (a.size() == b.size()) return a < b;
            return a.size() > b.size();
        });
        for (string str : dictionary) {
            int i = 0;
            for (char c : s) {
                if (i < str.size() && c == str[i]) ++i;
            }
            if (i == str.size()) return str;
        }
        return "";
    }
};

上面的方法中用到了 sort 排序,对于数组中单词的数量不是很多,但是长度特别长的情况很适用,但是如果对于单词数量特别多,但是每个都不长的话,排序的 O(nlgn) 时间复杂度可能就不是最好的方法了,也可以不用排序来做。我们遍历字典中的单词,然后还是跟上面的方法一样来验证当前单词是否能由字符串s通过删除字符来得到,如果能得到,而且单词长度大于等于结果 res 的长度,再看是否需要更新结果 res,有两种情况是必须要更新结果 res 的,一个是当前单词长度大于结果 res 的长度,另一种是当前单词长度和 res 相同,但是字母顺序小于结果 res,这两种情况下更新结果 res 即可,参见代码如下:

解法二:

class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) {
        string res = "";
        for (string str : dictionary) {
            int i = 0;
            for (char c : s) {
                if (i < str.size() && c == str[i]) ++i;
            }
            if (i == str.size() && str.size() >= res.size()) {
                if (str.size() > res.size() || str < res) {
                    res = str;
                }
            }
        }
        return res;
    }
};

Github 同步地址:

#524

类似题目:

Longest Word in Dictionary

参考资料:

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/

https://discuss.leetcode.com/topic/80876/10-lines-solutions-for-c

https://discuss.leetcode.com/topic/80799/short-java-solutions-sorting-dictionary-and-without-sorting/2

LeetCode All in One 题目讲解汇总(持续更新中...)

(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)

知识星球 喜欢请点赞,疼爱请打赏❤️~.~

微信打赏

|

Venmo 打赏


---|---

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant