Longest Word in Dictionary - Problem
Longest Word in Dictionary is a fascinating string manipulation problem that challenges you to find the longest word that can be built incrementally from other words in the dictionary.

Given an array of strings words representing an English dictionary, you need to return the longest word that can be constructed one character at a time by other words in the same dictionary. The key constraint is that each word must be built from left to right, adding one character at a time to form valid words that exist in the dictionary.

If multiple words have the same maximum length, return the one with the smallest lexicographical order. If no such word exists, return an empty string.

Example: Given ["w", "wo", "wor", "worl", "world"], the answer is "world" because it can be built as: w → wo → wor → worl → world, where each intermediate step exists in the dictionary.

Input & Output

example_1.py — Basic word building
$ Input: ["w", "wo", "wor", "worl", "world"]
Output: "world"
💡 Note: The word 'world' can be built one character at a time: w → wo → wor → worl → world, where each intermediate step exists in the dictionary.
example_2.py — Multiple valid words
$ Input: ["a", "banana", "app", "ap", "appl", "apple", "apply"]
Output: "apple"
💡 Note: Both 'apple' and 'apply' can be built incrementally (a → ap → app → appl → apple/apply), but 'apple' comes first lexicographically.
example_3.py — No buildable words
$ Input: ["abc", "def", "ghi"]
Output: ""
💡 Note: None of these words can be built one character at a time since their prefixes don't exist in the dictionary.

Visualization

Tap to expand
Building the Longest Word TowerwLevel 1woLevel 2worLevel 3worlLevel 4worldLevel 5🏆 TALLEST!Dictionary Validation✅ "w" exists in dictionary✅ "wo" exists in dictionary✅ "wor" exists in dictionary✅ "worl" exists in dictionary✅ "world" exists in dictionary🏗️BUILDINGEach level must bea valid word!
Understanding the Visualization
1
Foundation Check
Start with single letters (like 'w') that exist in our dictionary - these are our foundation blocks
2
Build Layer by Layer
Add one character at a time ('w' → 'wo' → 'wor') ensuring each intermediate word exists
3
Track the Tallest
Keep track of the longest valid word found, preferring alphabetically earlier words when tied
4
Use Smart Structure
A Trie organizes words like a building blueprint, making it easy to verify all construction steps
Key Takeaway
🎯 Key Insight: Use a Trie to organize words hierarchically, then DFS traverse only through valid word nodes to find the longest buildable path efficiently.

Time & Space Complexity

Time Complexity
⏱️
O(n × m)

Building Trie takes O(n × m) where n is number of words and m is average length, DFS is also O(n × m)

n
2n
Linear Growth
Space Complexity
O(n × m)

Space for Trie structure storing all characters of all words

n
2n
Linearithmic Space

Constraints

  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 30
  • words[i] consists of lowercase English letters only
  • All words[i] are unique
  • The answer is guaranteed to be unique, or empty string if no valid word exists
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen