Longest Word in Dictionary - Complete Solution Guide
Longest Word in Dictionary is LeetCode problem 720, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Example 1: Input: words = ["w","wo","wor","worl
Detailed Explanation
The problem asks us to find the longest word in a given list of words (dictionary) that can be built one character at a time by other words also present in the list. The words are built sequentially, meaning each new character is appended to a valid existing word in the dictionary. If multiple words satisfy this condition and have the same length, we must return the lexicographically smallest one. If no such word exists, return an empty string.
Solution Approach
The solution first sorts the input `words` lexicographically. It then iterates through the sorted `words` list, maintaining a set called `built_words` which stores words that can be built by sequentially adding characters from other words in the list. For each `word`, it checks if its prefix (all characters except the last one) exists in `built_words`. If the prefix exists, the current `word` is added to `built_words` and its length is compared to the current `result`. If the current `word` is longer than the `result`, or if it's the same length but lexicographically smaller (handled by sorting), the `result` is updated.
Step-by-Step Algorithm
- Step 1: Sort the input list of `words` lexicographically. This ensures the lexicographical tie-breaking rule is satisfied.
- Step 2: Initialize an empty set called `built_words` and add an empty string to it. The empty string represents the base case for building words.
- Step 3: Initialize an empty string called `result` to store the longest word found so far.
- Step 4: Iterate through each `word` in the sorted `words` list.
- Step 5: For each `word`, extract its prefix (the word without the last character).
- Step 6: Check if the prefix exists in the `built_words` set.
- Step 7: If the prefix exists in `built_words`, add the current `word` to the `built_words` set.
- Step 8: If the length of the current `word` is greater than the length of the `result`, update `result` to be the current `word`.
- Step 9: After iterating through all `words`, return the `result` string.
Key Insights
- Insight 1: Sorting the input words lexicographically allows us to prioritize smaller words and handle the lexicographical requirement in case of ties in length.
- Insight 2: Using a set to keep track of 'built' words efficiently allows us to check if a word's prefix exists in O(1) on average.
- Insight 3: The empty string is implicitly 'built', serving as the base case for single-character words.
Complexity Analysis
Time Complexity: O(NlogN)
Space Complexity: O(N)
Topics
This problem involves: Array, Hash Table, String, Trie, Sorting.
Companies
Asked at: Pinterest.