Longest Word in Dictionary - Problem
Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words.
If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.
Note that the word should be built from left to right with each additional character being added to the end of a previous word.
Input & Output
Example 1 — Basic Building
$
Input:
words = ["w","wo","wor","worl","world"]
›
Output:
"world"
💡 Note:
"world" can be built one character at a time: "w" → "wo" → "wor" → "worl" → "world". All intermediate words exist in the dictionary.
Example 2 — Lexicographic Tie
$
Input:
words = ["a","banana","app","appl","ap","apply","apple"]
›
Output:
"apple"
💡 Note:
Both "apple" and "apply" can be built (a → ap → app → appl → apple/apply), but "apple" is lexicographically smaller.
Example 3 — No Valid Words
$
Input:
words = ["hello","world"]
›
Output:
""
💡 Note:
Neither word can be built step by step. "hello" needs "h", "he", "hel", "hell" which don't exist. Same for "world".
Constraints
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length ≤ 30
- words[i] consists of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of dictionary words
2
Process
Find words that can be built character by character
3
Output
Longest buildable word (lexicographically smallest if tie)
Key Takeaway
🎯 Key Insight: A word can only be built if every single prefix exists in the dictionary
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code