524. Longest Word in Dictionary through Deleting

Posted apanda009

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了524. Longest Word in Dictionary through Deleting相关的知识,希望对你有一定的参考价值。

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

 

An alternate, more efficient solution which avoids sorting the dictionary:

public String findLongestWord(String s, List<String> d) {
    String longest = "";   // 用来比较的local
    for (String dictWord : d) {  // 题意: 顺序遍历
        int i = 0;               // 每次都得从target头部开始遍历
        for (char c : s.toCharArray())   // 判断字符串的每一个字母相等用toCharArray
            if (i < dictWord.length() && c == dictWord.charAt(i)) i++; 题意: 比较

        if (i == dictWord.length() && dictWord.length() >= longest.length()) //判断是否满足题意
            if (dictWord.length() > longest.length() || dictWord.compareTo(longest) < 0) // 是否满足两点, 更新global, 
                                          学会s.compareTo(ss) 来比较字典顺序
         longest = dictWord;
  }
  return longest;
}

Time Complexity: O(nk), where n is the length of string s and k is the number of words in the dictionary.

  

以上是关于524. Longest Word in Dictionary through Deleting的主要内容,如果未能解决你的问题,请参考以下文章

524. Longest Word in Dictionary through Deleting

524 Longest Word in Dictionary through Deleting

[LeetCode] 524. Longest Word in Dictionary through Deleting

LeetCode 524. Longest Word in Dictionary through Deleting (通过删除字母匹配到字典里最长单词)

leetcode 720 - Longest Word in Dictionary

720. Longest Word in Dictionary