成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Longest Word in Dictionary

econi / 2594人閱讀

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/77001.html

相關(guān)文章

  • [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 評(píng)論0 收藏0
  • [Leetcode] Word Break 單詞分解

    摘要:所以只要驗(yàn)證滿足這個(gè)條件,我們則可以確定這個(gè)較長(zhǎng)的字符串也是可分解的。同時(shí),我們用數(shù)組記錄下字符串長(zhǎng)度遞增時(shí)可分解的情況,以供之后使用,避免重復(fù)計(jì)算。當(dāng)遍歷完這個(gè)詞典并找出所有以第一個(gè)字母開頭的詞以后,我們進(jìn)入下一輪搜索。 Word Break I Given a string s and a dictionary of words dict, determine if s can ...

    Ververica 評(píng)論0 收藏0
  • leetcode140. Word Break II

    摘要:題目要求現(xiàn)在有一個(gè)非空字符串和一個(gè)非空的字典?,F(xiàn)在向中添加空格從而構(gòu)成一個(gè)句子,其中這個(gè)句子的所有單詞都存在與中。 題目要求 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 評(píng)論0 收藏0
  • [Leetcode] Alien Dictionary 外文字典

    摘要:拓?fù)渑判驈?fù)雜度時(shí)間空間思路首先簡(jiǎn)單介紹一下拓?fù)渑判?,這是一個(gè)能夠找出有向無環(huán)圖順序的一個(gè)方法假設(shè)我們有條邊,先將每個(gè)節(jié)點(diǎn)的計(jì)數(shù)器初始化為。最后,我們開始拓?fù)渑判?,從?jì)數(shù)器為的字母開始廣度優(yōu)先搜索。 Alien Dictionary There is a new alien language which uses the latin alphabet. However, the ord...

    pkhope 評(píng)論0 收藏0
  • [leetcode] Minimum Window Substring

    摘要:使用而不是因?yàn)槲覀冃枰氖亲钪?,中間值我們不在乎,所以一次收斂到最小。下面來三個(gè)需要查重并且記錄上次出現(xiàn)的位置,選擇以為例,走到用做檢查,發(fā)現(xiàn)出現(xiàn)過,把移到的下一個(gè)。是上個(gè)題目的簡(jiǎn)易版,或者特殊版。 這里聊一聊解一類問題,就是滿足某一條件的substring最值問題。最開始我們以Minimum Window Substring為例,并整理總結(jié)leetcode里所有類似題目的通解。 Gi...

    Pines_Cheng 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

econi

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<