Data Structures19 sections · 729 units
Open in Course

Problem - Longest Word in Dictionary

Trie with constraints

Given an array of strings, find the longest word that you can build one character at a time by other words in the array.

Example: For ["w","wo","wor","worl","world"][\text{"w"}, \text{"wo"}, \text{"wor"}, \text{"worl"}, \text{"world"}], return "world" because each prefix ("w", "wo", "wor", "worl") is also in the array.

If multiple answers exist, return the lexicographically smallest. This tests whether every prefix of a word exists in the dictionary. A natural trie application. Constraints: up to 10001000 words, each length up to 3030.