#720
Longest Word in Dictionary
specialist · 860 · lc medium +31 · verified · 54.5% accepted · 2,080 likes · top 47%
Description
Given an array of strings words, find the longest word that can be constructed one character at a time using other words in words (each prefix of the word must itself exist in words). Characters are added left to right.
If multiple words qualify, return the lexicographically smallest one. Return an empty string if no such word exists.
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".
Code
1
2
3