720. Longest Word in Dictionary
Description
Given an array of strings words
representing an English Dictionary, return 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.
Note that the word should be built from left to right with each additional character being added to the end of a previous word.
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".
Constraints:
1 <= words.length <= 1000
1 <= words[i].length <= 30
words[i]
consists of lowercase English letters.
Solution
longest-word-in-dictionary.py
class TrieNode:
def __init__(self):
self.mp = [None] * 26
self.end = False
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
self.ans = collections.defaultdict(int)
def insert(self, word: str):
"""
Inserts a word into the trie.
"""
curr = self.root
n = len(word)
for i in range(n):
index = ord(word[i]) - ord('a')
if not curr.mp[index]:
curr.mp[index] = TrieNode()
curr = curr.mp[index]
curr.end = True
def search(self):
self.searchR(self.root)
def searchR(self, node, word = "", level = 0):
"""
Returns if the word is in the trie.
"""
if node.end and not self.ans[level]:
self.ans[level] = word
if self.isLastNode(node): return
for i in range(26):
if node.mp[i] and node.mp[i].end:
word += chr(97+i)
self.searchR(node.mp[i], word, level + 1)
word = word[:-1]
def isLastNode(self, node):
for i in range(26):
if node.mp[i]: return False
return True
class Solution:
def longestWord(self, words: List[str]) -> str:
trie = Trie()
for w in words:
trie.insert(w)
trie.search()
res = trie.ans
return res[max(res)]