問題:node
Given a list of strings words
representing an English Dictionary, find the longest word in words
that can be built one character at a time by other words in words
. If there is more than one possible answer, return the longest word with the smallest lexicographical order.數組
If there is no answer, return the empty string.app
Example 1:ui
Input: words = ["w","wo","wor","worl", "world"] Output: "world" Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
Example 2:spa
Input: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] Output: "apple" Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
Note:code
words
will be in the range [1, 1000]
.words[i]
will be in the range [1, 30]
.解決:排序
【題意】給定一個字典,是個字符串數組,從單個字符開始拼,返回最長能組成單詞,注意中間生成的字符串也要在字典中存在,並且當組成的單詞長度相等時,返回字母順序小的那個。字符串
① 先將單詞按照字典序排序,爲了快速的查找某個單詞是否在字典中存在,將字典中的單詞存儲到set集合中。input
class Solution { //42ms
public String longestWord(String[] words) {
Arrays.sort(words);//按照字典序排序
Set<String> set = new HashSet<>();//用於存儲字典中已經存在的字符
String res = "";
for (String word : words){
if (word.length() == 1 || set.contains(word.substring(0,word.length() - 1))){
res = word.length() > res.length() ? word : res;
set.add(word);
}
}
return res;
}
}string
② 使用前綴樹,bfs查找。
class Solution { //20ms
class TrieNode{
TrieNode[] children;
boolean isWord;
String word;
public TrieNode(){
children = new TrieNode[26];
}
}
class Trie{
private TrieNode root;
public Trie(){
root = new TrieNode();
}
public void insert(String word){
TrieNode node = root;
for (int i = 0;i < word.length();i ++){
int index = word.charAt(i) - 'a';
if (node.children[index] == null){
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isWord = true;
node.word = word;
}
public String findLongestWord(){
String res = null;
Queue<TrieNode> queue = new LinkedList<>();
queue.offer(root);
while(! queue.isEmpty()){
int count = queue.size();
for (int i = 0;i < count;i ++){
TrieNode node = queue.poll();
for (int j = 25;j >= 0;j --){
if (node.children[j] != null && node.children[j].isWord){
res = node.children[j].word;
queue.offer(node.children[j]);
}
}
}
}
return res;
}
}
public String longestWord(String[] words) {
Trie trie = new Trie();
for (String word : words){
trie.insert(word);
}
return trie.findLongestWord();
}
}
③ 前綴樹+dfs查找
class Solution { //18ms class TrieNode{ TrieNode[] children; String word; public TrieNode(){ children = new TrieNode[26]; } } TrieNode root = new TrieNode(); public String longestWord(String[] words){ root.word = ""; for (String word : words){ insert(word); } TrieNode cur = root; return dfs(cur); } public void insert(String word){ TrieNode cur = root; for (char c : word.toCharArray()){ int index = c - 'a'; if (cur.children[index] == null){ cur.children[index] = new TrieNode(); } cur = cur.children[index]; } cur.word = word; } public String dfs(TrieNode cur){ String res = cur.word; for (int i = 0;i < 26;i ++){ if (cur.children[i] != null && cur.children[i].word != null){ String tmp = dfs(cur.children[i]); if (tmp.length() > res.length()){ res = tmp; } } } return res; } }