Advertisement
1988coder

720. Longest Word in Dictionary

Oct 6th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. /**
  2. Using Trie.
  3. Save all the words in Trie.
  4. Now for each word check if there is an end after each character.
  5.  
  6. Time Complexity = O(n * l)
  7. n = Number of words.
  8. l = Average length of the words.
  9.  
  10. Space Complexity = O(n * l)
  11. */
  12. class Solution {
  13.     class TrieNode {
  14.         HashMap<Character, TrieNode> map;
  15.         boolean isEnd;
  16.        
  17.         TrieNode() {
  18.             map = new HashMap<>();
  19.             isEnd = false;
  20.         }
  21.     }
  22.    
  23.     public String longestWord(String[] words) {
  24.         if (words == null || words.length == 0) {
  25.             return "";
  26.         }
  27.        
  28.         TrieNode root = new TrieNode();
  29.         for (String w : words) {
  30.             TrieNode cur = root;
  31.             for (char c : w.toCharArray()) {
  32.                 if (!cur.map.containsKey(c)) {
  33.                     cur.map.put(c, new TrieNode());
  34.                 }
  35.                 cur = cur.map.get(c);
  36.             }
  37.             cur.isEnd = true;
  38.         }
  39.        
  40.         String result = "";
  41.        
  42.         for (String w : words) {
  43.             if (result.length() > w.length() || (result.length() == w.length() && result.compareTo(w) < 0)) {
  44.                 continue;
  45.             }
  46.            
  47.             TrieNode cur = root;
  48.            
  49.             for (char c : w.toCharArray()) {
  50.                 cur = cur.map.get(c);
  51.                 if (!cur.isEnd) {
  52.                     break;
  53.                 }
  54.             }
  55.            
  56.             if (cur.isEnd) {
  57.                 result = w;
  58.             }
  59.         }
  60.         return result;
  61.     }
  62. }
  63.  
  64. /**
  65. Sort the words alphabetically, therefore shorter words always comes before longer words.
  66. Iterate over the sorted list, populate the words that can be built in the HashSet. Any prefix of a word must comes before that word. If not, then ignore the word.
  67.  
  68. Time Complexity = O(n*l * log(n)) - Sort the words array. and iterate over each character to find the result
  69. n = Number of words.
  70. l = Average length of the words.
  71.  
  72. Space Complexity = O(n * l)
  73. */
  74. // class Solution {
  75. //     public String longestWord(String[] words) {
  76. //         if (words == null || words.length == 0) {
  77. //             return "";
  78. //         }
  79. //         if (words.length == 1) {
  80. //             if (words[0].length() == 1) {
  81. //                 return words[0];
  82. //             } else {
  83. //                 return "";
  84. //             }
  85. //         }
  86.        
  87. //         Arrays.sort(words);
  88.        
  89. //         HashSet<String> set = new HashSet<>();
  90. //         String result = "";
  91.        
  92. //         for (String w : words) {
  93. //             if (w.length() == 1 || set.contains(w.substring(0, w.length()-1))) {
  94. //                 set.add(w);
  95. //                 if (w.length() > result.length()) {
  96. //                     result = w;
  97. //                 }
  98. //             }
  99. //         }
  100. //         return result;
  101. //     }
  102. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement