Longest Word in Dictionary
2018-01-17 00:00
225 查看
问题:
Given a list of strings
If there is no answer, return the empty string.
Example 1:
Example 2:
Note:
All the strings in the input will only contain lowercase letters.
The length of
The length of
解决:
【题意】给定一个字典,是个字符串数组,从单个字符开始拼,返回最长能组成单词,注意中间生成的字符串也要在字典中存在,而且当组成的单词长度相等时,返回字母顺序小的那个。
① 先将单词按照字典序排序,为了快速的查找某个单词是否在字典中存在,将字典中的单词存储到set集合中。
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;
}
}
② 使用前缀树,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;
}
}
Given a list of strings
wordsrepresenting an English Dictionary, find the longest word in
wordsthat 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.
Example 1:
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:
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:
All the strings in the input will only contain lowercase letters.
The length of
wordswill be in the range
[1, 1000].
The length of
words[i]will be in the range
[1, 30].
解决:
【题意】给定一个字典,是个字符串数组,从单个字符开始拼,返回最长能组成单词,注意中间生成的字符串也要在字典中存在,而且当组成的单词长度相等时,返回字母顺序小的那个。
① 先将单词按照字典序排序,为了快速的查找某个单词是否在字典中存在,将字典中的单词存储到set集合中。
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;
}
}
② 使用前缀树,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;
}
}
相关文章推荐
- 720. Longest Word in Dictionary
- leetcode720——Longest Word in Dictionary
- 524. Longest Word in Dictionary through Deleting
- [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
- Longest Word in Dictionary问题及解法
- leetcode524~Longest Word in Dictionary through Deleting
- LeetCode.720 Longest Word in Dictionary
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
- Longest Word in Dictionary through Deleting问题及解法
- Longest Word in Dictionary
- HashTable-720-Longest Word in Dictionary
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
- Longest Word in Dictionary through Deleting
- 720. Longest Word in Dictionary
- 524[Medium] :Longest Word in Dictionary through Deleting
- [LeetCode] Longest Word in Dictionary through Deleting
- LeetCode——Longest Word in Dictionary through Deleting
- Leetcode之Longest Word in Dictionary through Deleting 问题
- 524. Longest Word in Dictionary through Deleting
- Longest Word in Dictionary through Deleting