资讯专栏INFORMATION COLUMN

[LeetCode] Longest Word in Dictionary

econi / 2300人阅读

Problem

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].

Solution
class Solution {
    class Trie {
        Node root;
        class Node {
            boolean isWord;
            Node[] children = new Node[26];
        }
        
        public Trie() {
            root = new Node();
            root.isWord = true;
        }
        
        void insert (String str) {
            Node node = root;
            for (char ch: str.toCharArray()) {
                int index = ch - "a";
                if (node.children[index] == null) {
                    node.children[index] = new Node();
                }
                node = node.children[index];
            }
            node.isWord = true;
        }
        
        boolean hasWord(String word) {
            Node node = root;
            for (char ch: word.toCharArray()) {
                int index = ch - "a";
                if (node.children[index] == null || !node.isWord) return false;
                node = node.children[index];
            }
            return true;
        }
    }
    public String longestWord(String[] words) {
        Trie trie = new Trie();
        for (String word: words) {
            trie.insert(word);
        }
        String res = "";
        for (String word: words) {
            if (trie.hasWord(word) && 
               (word.length() > res.length() || (word.length() == res.length() && word.compareTo(res) < 0))) {
                res = word;
            }
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77001.html

相关文章

  • [LintCode] Longest Words

    Problem Given a dictionary, find all of the longest words in the dictionary. Example Given { dog, google, facebook, internationalization, blabla } the longest words are(is) [internationaliz...

    roadtogeek 评论0 收藏0
  • [Leetcode] Word Break 单词分解

    摘要:所以只要验证满足这个条件,我们则可以确定这个较长的字符串也是可分解的。同时,我们用数组记录下字符串长度递增时可分解的情况,以供之后使用,避免重复计算。当遍历完这个词典并找出所有以第一个字母开头的词以后,我们进入下一轮搜索。 Word Break I Given a string s and a dictionary of words dict, determine if s can ...

    Ververica 评论0 收藏0
  • leetcode140. Word Break II

    摘要:题目要求现在有一个非空字符串和一个非空的字典。现在向中添加空格从而构成一个句子,其中这个句子的所有单词都存在与中。 题目要求 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence ...

    huayeluoliuhen 评论0 收藏0
  • [Leetcode] Alien Dictionary 外文字典

    摘要:拓扑排序复杂度时间空间思路首先简单介绍一下拓扑排序,这是一个能够找出有向无环图顺序的一个方法假设我们有条边,先将每个节点的计数器初始化为。最后,我们开始拓扑排序,从计数器为的字母开始广度优先搜索。 Alien Dictionary There is a new alien language which uses the latin alphabet. However, the ord...

    pkhope 评论0 收藏0
  • [leetcode] Minimum Window Substring

    摘要:使用而不是因为我们需要的是最值,中间值我们不在乎,所以一次收敛到最小。下面来三个需要查重并且记录上次出现的位置,选择以为例,走到用做检查,发现出现过,把移到的下一个。是上个题目的简易版,或者特殊版。 这里聊一聊解一类问题,就是满足某一条件的substring最值问题。最开始我们以Minimum Window Substring为例,并整理总结leetcode里所有类似题目的通解。 Gi...

    Pines_Cheng 评论0 收藏0

发表评论

0条评论

econi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<