Tries Prefix trees
424. Longest Word in Dictionary
You are given a list of lowercase words, words. Return the longest word in the list that can be built one
letter at a time, where each step along the way is also a word in the list.
So a word can be built when all its shorter prefixes are in words: its first letter on its own, its
first two letters, and so on. For example, "cab" can be built if the list also holds "c" and "ca". A
one-letter word can always be built.
If several words of the greatest length can be built, return the one that comes first in alphabetical order.
If no word can be built, return the empty string. Answers are shown in double quotes, so an empty answer
shows as "".
Example 1
Input: words = ["c", "ca", "cab", "cabs", "cart", "carts"]
"cabs"Explanation: "c", "ca" and "cab" are all in the list, so "cabs" can be built. "carts" is longer, but "car" is not in the list, so neither "cart" nor "carts" can be built.
Example 2
Input: words = ["y", "ye", "yes", "o", "on", "one", "yet"]
"one"Explanation: "yes", "yet" and "one" can all be built, and each has three letters. "one" comes first in alphabetical order.
Example 3
Input: words = ["it", "its", "ice"]
""Explanation: No word in the list has one letter, so no word can start a chain. "its" would need "i" as well as "it".
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ words.length ≤ 1051 ≤ words[i].length ≤ 30- All the words together hold at most
106letters. words[i]holds only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.