Longest Word in Dictionary in Python

Finding the longest word in a dictionary that can be built one character at a time is a classic problem that can be solved efficiently using a Trie (Prefix Tree) data structure. The goal is to find the longest word where each prefix also exists in the dictionary.

For example, given the dictionary ["h","he","hel","hell","hello"], the word "hello" can be built step by step since "h", "he", "hel", and "hell" all exist in the dictionary.

Algorithm Overview

The solution uses a Trie to store all words and then checks if each word can be built incrementally ?

  1. Build a Trie from all words in the dictionary
  2. For each word, verify that all its prefixes exist in the Trie
  3. Return the longest valid word (lexicographically smallest if there's a tie)

Implementation

class Solution:
    def longestWord(self, words):
        self.trie = {}
        
        def insert(word):
            now = self.trie
            for c in word:
                if c not in now:
                    now[c] = {'#': False}
                now = now[c]
            now['#'] = True
        
        def search(word):
            now = self.trie
            for c in word:
                if '#' not in now or not now['#']:
                    return False
                now = now[c]
            return now['#']
        
        # Insert all words into the Trie
        for word in words:
            insert(word)
        
        ans = ""
        for word in words:
            if search(word) and (len(word) > len(ans) or (len(word) == len(ans) and word < ans)):
                ans = word
        
        return ans

# Test the solution
ob = Solution()
result = ob.longestWord(["h", "he", "hel", "hell", "hello"])
print(result)
hello

How It Works

The algorithm works in three phases ?

  • Insert Phase: All words are inserted into a Trie structure where each node contains a '#' flag indicating if it represents a complete word
  • Search Phase: For each word, we traverse the Trie checking that every prefix (character by character) forms a complete word
  • Selection Phase: Among valid words, we select the longest one, with lexicographical ordering as the tiebreaker

Another Example

# Test with a more complex dictionary
ob = Solution()
words = ["a", "banana", "app", "appl", "ap", "apply", "application"]
result = ob.longestWord(words)
print(f"Input: {words}")
print(f"Output: {result}")
Input: ['a', 'banana', 'app', 'appl', 'ap', 'apply', 'application']
Output: a

In this example, only "a" qualifies because words like "app" cannot be built incrementally (we need "ap" and "a" first, but "ap" needs "a" and "p", where "p" doesn't exist).

Time Complexity

  • Time: O(N × M) where N is the number of words and M is the average word length
  • Space: O(N × M) for the Trie structure

Conclusion

This Trie-based approach efficiently finds the longest buildable word by ensuring all prefixes exist in the dictionary. The solution handles ties by returning the lexicographically smallest word among those with maximum length.

Updated on: 2026-03-25T08:48:27+05:30

612 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements