5 Best Ways to Find the Length of the Longest Diminishing Word Chain in Python

Rate this post

πŸ’‘ Problem Formulation: A diminishing word chain is a sequence of words where each word is formed by removing a single letter from the previous word. The challenge is to write a Python program that takes a list of words and returns the length of the longest such chain that can be created from them. An example input could be [‘word’, ‘ward’, ‘war’, ‘par’, ‘paw’], with the desired output being 4 for the chain ‘ward’ -> ‘ward’ -> ‘war’ -> ‘par’.

Method 1: Recursive Backtracking

This method uses a recursive backtracking algorithm to explore all possible word chains and find the maximum length. A helper function checks if a word can follow another in the sequence. The algorithm investigates all paths recursively and backtracks when no longer words can follow in the chain.

Here’s an example:

def can_follow(word1, word2):
    if len(word1) != len(word2) + 1:
        return False
    for i in range(len(word1)):
        if word1[:i] + word1[i+1:] == word2:
            return True
    return False

def find_max_chain(words, current_word=None):
    max_chain = 0
    for word in words:
        if current_word is None or can_follow(current_word, word):
            next_words = words.copy()
            current_chain = 1 + find_max_chain(next_words, word)
            max_chain = max(max_chain, current_chain)
    return max_chain

words = ['word', 'ward', 'war', 'par', 'paw']

Output: 4

This snippet defines the function can_follow() which ensures that one word can be transformed into another by removing one letter, and the function find_max_chain() which recursively builds diminishing word chains and calculates their length, respectively. This method effectively finds the longest chain, but can be slow for large word lists due to its recursive nature.

Method 2: Dynamic Programming

Dynamic Programming (DP) is an optimization technique to solve problems by combining solutions of subproblems. This method uses DP to store the lengths of longest chains ending with each word to avoid recalculating them. It sorts the words by length and uses them to build up to the solution.

Here’s an example:

def longest_diminishing_word_chain_dp(words):
    words.sort(key=len, reverse=True)
    longest_chain = {word: 1 for word in words}
    for word in words:
        for i in range(len(word)):
            smaller_word = word[:i] + word[i+1:]
            if smaller_word in longest_chain:
                longest_chain[smaller_word] = max(longest_chain[smaller_word], longest_chain[word] + 1)
    return max(longest_chain.values())

words = ['word', 'ward', 'war', 'par', 'paw']

Output: 4

The code initializes a dictionary longest_chain to keep track of the longest chain that ends with each word. It iterates over each word, generating all possible preceding words in the chain, and updates the dictionary accordingly. It is much faster than the recursive approach, especially for large datasets.

Method 3: Graph Theory

By representing the words as nodes and the possible transitions as edges, this problem can be modeled as finding the longest path in a Directed Acyclic Graph (DAG). We use topological sorting to ensure each word is only considered after all possible preceding words have been processed.

Here’s an example:

from collections import defaultdict, deque

def longest_path_DAG(words):
    graph = defaultdict(list)
    in_degree = defaultdict(int)
    for word in words:
        for i in range(len(word)):
            smaller_word = word[:i] + word[i+1:]
            if smaller_word in words:
                in_degree[word] += 1

    # Topological Sorting
    zero_in_degree_queue = deque([word for word in words if in_degree[word] == 0])
    longest_path = {word: 1 for word in words}
    while zero_in_degree_queue:
        current = zero_in_degree_queue.popleft()
        for neighbor in graph[current]:
            longest_path[neighbor] = max(longest_path[neighbor], longest_path[current] + 1)
            in_degree[neighbor] -= 1
            if in_degree[neighbor] == 0:
    return max(longest_path.values())

words = ['word', 'ward', 'war', 'par', 'paw']

Output: 4

The longest_path_DAG() function constructs a DAG from the words and computes the longest path using topological sorting. This method can be more efficient than recursive methods for large datasets, but the complexity of graph construction must be taken into account.

Method 4: Iterative Approach with Word Predecessors

This method relies on building a table of word predecessors for each word and then iteratively scanning through the table to find the longest chain. This is a good solution for datasets that are not too large and where recursion stack limits might be a concern. The table is used to keep track of which words can come before a given word in a chain.

Here’s an example:

def longest_chain_iterative(words):
    word_set = set(words)
    predecessors = {word: [] for word in word_set}
    for word in word_set:
        for i in range(len(word)):
            predecessor = word[:i] + word[i+1:]
            if predecessor in word_set:

    max_chain_length = 0
    for word in word_set:
        chain_length = 1
        queue = predecessors[word]
        while queue:
            chain_length += 1
            current = queue.pop()
            queue.extend(predecessors[current] if predecessors[current] else [])
        max_chain_length = max(max_chain_length, chain_length)
    return max_chain_length

words = ['word', 'ward', 'war', 'par', 'paw']

Output: 4

The function longest_chain_iterative() builds a set of words and a dictionary mapping words to their possible predecessors. It then uses an iterative process to determine the maximum length of any chain. Although generally efficient, this method might be slower for datasets with a large number of predecessors for each word.

Bonus One-Liner Method 5: Using List Comprehensions and Sorting

This one-liner uses Python’s powerful list comprehensions and sorting capabilities to create a compact solution. It is not recommended for large inputs or for clarity but is a neat demonstration of Python’s expressive power.

Here’s an example:

words = ['word', 'ward', 'war', 'par', 'paw']
print(max((lambda f, *a: len(sorted(a, key=len)) and f(f, *[w for i, c in enumerate(sorted(a, key=len)[0]) for w in a if w[:i]+w[i+1:] == c]))(lambda f, *a: a and max(a)+1 or 0, *words))

Output: 4

This one-liner defines an anonymous recursive function using lambda and calls it immediately with a sequence of words. It uses list comprehension to sort the words by length and to find all possible predecessors. Unsuitable for large datasets, this method is primarily of academic interest due to its reduced readability and potential for stack overflow errors.


  • Method 1: Recursive Backtracking. This exhaustive search can handle complex chaining rules. However, it has exponential time complexity and can hit recursion limits.
  • Method 2: Dynamic Programming. Offers improved performance through memoization. It scales better than recursion but requires sorting, which adds overhead.
  • Method 3: Graph Theory. Ideal for large inputs as it leverages graph data structures and algorithms. It may be complex to implement and maintain.
  • Method 4: Iterative Approach with Word Predecessors. A balanced method in terms of complexity and efficiency. May struggle with large inputs with many predecessors.
  • Bonus One-Liner Method 5: Showcases the concise power of Python at a significant cost to clarity and performance. Its academic or didactic value outweighs its practicality.