Longest Word in Dictionary

Hard
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Longest Word in Dictionary

Approach

Insert all words, then DFS only through nodes that are complete words and keep the longest valid chain.

Complexity Analysis

Time
O(W * L)
Space
O(W * L)

Pattern

Trie Prefix Validity

Why It Works

A valid chain requires every prefix to be present; DFS restricted to terminal nodes automatically explores exactly those candidates.

Updated Feb 2026