字典中的最长单词 Longest Word in Dictionary

问题:

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.

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 words will 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;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1608201

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值