|
| 1 | +public class LeetCode_720_085 { |
| 2 | +} |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | + * @Package: |
| 7 | + * @ClassName: LongestWordInDictionary |
| 8 | + * @Description: 给出一个字符串数组words组成的一本英语词典。从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成。 |
| 9 | + * **************若其中有多个可行的答案,则返回答案中字典序最小的单词。 |
| 10 | + * **************若无答案,则返回空字符串。 |
| 11 | + * @leetcode url:https://leetcode-cn.com/problems/longest-word-in-dictionary/ |
| 12 | + * @Author: wangzhao |
| 13 | + * @Date: 2019-05-11 21:21:44 |
| 14 | + * @Version: 1.0.0 |
| 15 | + * @Since: 1.8 |
| 16 | + **/ |
| 17 | +class LongestWordInDictionary { |
| 18 | + |
| 19 | + private String longestWord=""; |
| 20 | + public String longestWord(String[] words) { |
| 21 | + |
| 22 | + if (words == null || words.length == 0) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + Trie trie = new Trie(); |
| 26 | + for (String word : words) { |
| 27 | + trie.insert(word); |
| 28 | + } |
| 29 | + |
| 30 | + search(new StringBuffer(),trie.nodes); |
| 31 | + |
| 32 | + return longestWord; |
| 33 | + } |
| 34 | + |
| 35 | + private void search(StringBuffer sb,TreeNode4[] node4s) { |
| 36 | + if (node4s == null || node4s.length == 0) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + for (TreeNode4 node4 : node4s) { |
| 41 | + if (node4 != null && node4.end) { |
| 42 | + sb.append(node4.val); |
| 43 | + if (sb.length()>longestWord.length()){ |
| 44 | + longestWord=sb.toString(); |
| 45 | + } |
| 46 | + search(sb,node4.children); |
| 47 | + sb.deleteCharAt(sb.length()-1); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | +// String[] words = {"w", "wo", "wor", "worl", "world"}; |
| 56 | + String[] words= {"a", "banana", "app", "appl", "ap", "apply", "apple"}; |
| 57 | + String longestWord = new LongestWordInDictionary().longestWord(words); |
| 58 | + System.out.println(longestWord); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class Trie { |
| 63 | + |
| 64 | + TreeNode4[] nodes; |
| 65 | + |
| 66 | + public Trie() { |
| 67 | + this.nodes = new TreeNode4[26]; |
| 68 | + } |
| 69 | + |
| 70 | + public void insert(String word) { |
| 71 | + if (word == null || word.length() == 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + insert(0, word.toCharArray(), nodes); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + private void insert(int i, char[] chars, TreeNode4[] children) { |
| 80 | + |
| 81 | + int idx = chars[i] - 'a'; |
| 82 | + |
| 83 | + if (children[idx] == null) { |
| 84 | + children[idx] = new TreeNode4(chars[i]); |
| 85 | + } |
| 86 | + |
| 87 | + if (i == chars.length - 1) { |
| 88 | + children[idx].end = true; |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (children[idx].children == null) { |
| 93 | + children[idx].children = new TreeNode4[26]; |
| 94 | + } |
| 95 | + |
| 96 | + insert(i + 1, chars, children[idx].children); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +class TreeNode4 { |
| 101 | + |
| 102 | + char val; |
| 103 | + boolean end; |
| 104 | + TreeNode4[] children; |
| 105 | + |
| 106 | + public TreeNode4(char val) { |
| 107 | + this.val = val; |
| 108 | + } |
| 109 | +} |
0 commit comments