Longest Word in Dictionary

hard

Find the longest word that can be built one character at a time

Longest Word in Dictionary

Key Insight

Only traverse trie nodes marked as end of word; every prefix on the route must be valid.

Step 1Insert Words
Array
w
0
wo
1
wor
2
worl
3
world
4
hello
5
hell
6
he
7
HashMap
w:1
wo:2
wor:3
worl:4
world:5
h:1
he:2

Mark terminals for all words in trie.

1 / 3

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Longest Word in Dictionary

Only traverse trie nodes marked as end of word; every prefix on the route must be valid.

  1. Insert Words

    Mark terminals for all words in trie.

  2. Validate Prefixes

    Skip branches where prefix is not terminal (hello has missing intermediate terminal at h/e/l/l)

  3. Select Best Answer

    Compare valid candidates and keep longest, then lexicographically smallest on ties.