LeetCode--720--easy--LongestWordInDictionary

package com.odyssey.app.algorithm.lc.trie;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Dingsheng Huang
 * @projectName app
 * @date 2020/3/12 18:11
 *
 * 720
 * easy
 * https://leetcode.com/problems/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].
 *
 */
public class LongestWordInDictionary {

    /**
     * approach 1: construct trie
     */
    private String result = "";
    public String longestWord(String[] words) {
        TrieNode root = new TrieNode();
        root.word = "-";
        for (String word : words) {
            root.insert(word, root);
        }
        dfs(root);
        return result;
    }

    /**
     * ["a","banana","app","appl","ap","apply","apple"]
     */
    private void dfs(TrieNode root) {
        if (root == null || root.word.length() == 0) {
            return;
        }
        if (root.word != "-") {
            if (result.length() < root.word.length()) {
                result = root.word;
            } else if (result.length() == root.word.length()) {
                result = result.compareTo(root.word) < 0 ? result : root.word;
            }
        }
        for (TrieNode node : root.children) {
            dfs(node);
        }
    }

    // construct trie , hand write
    class TrieNode {
        TrieNode[] children = new TrieNode[26];
        String word = "";

        void insert(String s, TrieNode root) {
            TrieNode currNode = root;
            char[] chs = s.toCharArray();
            for (int i = 0; i < chs.length; i++) {
                int idx = chs[i] - 'a';
                if (currNode.children[idx] == null) {
                    currNode.children[idx] = new TrieNode();
                }
                currNode = currNode.children[idx];
            }
            currNode.word = s;
        }
    }


    /**
     * approach 2: use hash set
     * @param words
     * @return
     */
    public String longestWord2(String[] words) {
        Set<String> set = new HashSet<>();
        Arrays.sort(words);
        String result = "";
        for (String word : words) {
            String pre = word.substring(0, word.length() - 1);
            if (set.contains(pre) || word.length() == 1) {
                if (word.length() > result.length()) {
                    result = word;
                }
                set.add(word);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println("ewqz".compareTo("yodn"));
    }

}
发布了187 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/huangdingsheng/article/details/104825844
720