Given an array of strings `words`, return the longest word in `words` that can be built one character at a time by other words in `words`: every prefix of it (of length 1, 2, ...) must also be in `words`. If there are several answers of the same length, return the one that is lexicographically smallest. If no word can be built, return the empty string.
Given an array of strings words, return the longest word in words that can be built one character at a time by other words in words: every prefix of it (of length 1, 2, ...) must also be in words. If there are several answers of the same length, return the one that is lexicographically smallest. If no word can be built, return the empty string.
Example 1:
Input: {"words":["w","wo","wor","worl","world"]}
Output: world
Constraints:
1≤words.length≤1000
1≤words[i].length≤30
words[i] consists of lowercase English letters.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"words":["w","wo","wor","worl","world"]}
Start with an empty trie. Each of the 5 words will be added as a path of letters, with its last node marked as a word end.