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","world"] Output: "world" Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
Example 2:
Input: words = ["a","banana","app","appl","ap","apply","apple"] Output: "apple" Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
Constraints:
1 <= words.length <= 1000
1 <= words[i].length <= 30
words[i]
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the longest word that can be built from other words involves checking every single word in the dictionary to see if it meets the criteria. We'll examine each word and methodically determine if it can be constructed by combining other words from the provided list. This is done by exhaustively checking all possible combinations of words.
Here's how the algorithm would work step-by-step:
def longest_word_in_dictionary_brute_force(words):
longest_buildable_word = ""
for current_word in words:
if can_be_built(current_word, words):
#Check if this buildable word is longer or same length but lexicographically smaller
if len(current_word) > len(longest_buildable_word) or \
(len(current_word) == len(longest_buildable_word) and current_word < longest_buildable_word):
longest_buildable_word = current_word
return longest_buildable_word
def can_be_built(target_word, words):
if target_word == "":
return True
#Iterate through all possible word combinations
for i in range(1 << len(words)):
combined_word = ""
for j in range(len(words)):
if (i >> j) & 1:
combined_word += words[j]
#Check if any combination equals target
if combined_word == target_word:
# If we're comparing the word against itself, we must exclude it from the list to check
if target_word not in words or words.count(target_word) > 1:
return True
else:
words_copy = words.copy()
words_copy.remove(target_word)
if target_word in words_copy and words_copy.count(target_word) > 0:
return True
else:
continue
return False
The best way to find the longest word from a dictionary that can be built one character at a time is to first organize the dictionary and then search strategically. We'll sort the words to make checking easier and then build our potential longest word character by character.
Here's how the algorithm would work step-by-step:
def longest_word(dictionary):
dictionary.sort(key=lambda word: (len(word), word))
longest_word_found = ""
valid_words = set()
for current_word in dictionary:
# Words of length 1 are inherently valid.
if len(current_word) == 1:
valid_words.add(current_word)
if len(current_word) > len(longest_word_found):
longest_word_found = current_word
else:
# Check if the word can be built from a shorter word.
prefix = current_word[:-1]
if prefix in valid_words:
valid_words.add(current_word)
# Update the longest word if applicable.
if len(current_word) > len(longest_word_found):
longest_word_found = current_word
return longest_word_found
Case | How to Handle |
---|---|
Empty dictionary array | Return an empty string immediately as there are no words to process. |
Dictionary contains only empty strings | Return the empty string, as it's the longest valid word constructible. |
No word in dictionary can be built from other words (all letters mixed up) | Return an empty string, indicating no valid word can be constructed. |
Dictionary contains only one word | If the word has length 1, return it, else check if its prefixes exist (which they don't), return ''. |
Dictionary contains extremely long words | Ensure the solution doesn't cause stack overflow errors due to excessive recursion by utilizing iterative methods. |
Dictionary contains words with non-alphabetic characters | Preprocess the dictionary to remove or ignore words containing non-alphabetic characters (or throw an error). |
Multiple words in dictionary are of same maximum length and valid | Return the lexicographically smallest word among all valid words of the maximum length. |
Integer overflow when calculating the word length | Use appropriate data types or length limits to prevent potential integer overflow when storing word length. |