Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-114

Trie Longest Word Building

Problem

Find the longest word that can be built one character at a time from the given words.

A word is buildable when every proper prefix of it is also in the list. Ties on length are broken by choosing the lexicographically smallest word.

Return the longest buildable word, or "" when none exists.

Hint

A single-letter word is always buildable if it appears in the list.

Test cases

case 1
in: [["w","wo","wor","worl","world"]]
expected: "world"
case 2
in: [["a","banana","app","appl","ap","apply","apple"]]
expected: "apple"
case 3
in: [["a","ab","abc"]]
expected: "abc"
case 4
in: [[]]
expected: ""
case 5
in: [["b","ba"]]
expected: "ba"
Starter codePython
def longest_word_built(words):
    # Your code here
    pass

5 test cases · run your code against them in the editor