Program to find length of longest diminishing word chain in Python?

Suppose we have a list of valid words and a string s. We need to find the length of the longest chain of diminishing words that can be generated by starting at s and removing single letters while keeping valid words.

A diminishing word chain is formed by repeatedly removing one character from a word to create another valid word from the given list.

Example

If the input is words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"] and s = "limit", the output will be 4. We can form the chain: "limit" ? "limi" ? "lii" ? "li".

Algorithm

We use a recursive approach with the following steps:

  • Find the starting word in the word list
  • For each character position, create a new word by removing that character
  • Recursively find the longest chain from the new word
  • Return the maximum chain length found

Implementation

class Solution:
    def solve(self, words, s):
        max_num = 0
        for word in words:
            if word == s:
                for j in range(len(s)):
                    new_word = s[:j] + s[j + 1:]
                    max_num = max(1 + self.solve(words, new_word), max_num)
        return max_num

# Test the solution
ob = Solution()
words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"]
s = "limit"
result = ob.solve(words, s)
print(f"Longest diminishing word chain length: {result}")
Longest diminishing word chain length: 4

How It Works

The algorithm works as follows:

  1. Base case: If the current word is not in the word list, return 0
  2. Recursive case: For each position in the current word, remove that character
  3. Chain building: Recursively find the longest chain starting from the modified word
  4. Maximum selection: Return 1 plus the maximum chain length found

Optimized Version with Memoization

For better performance, we can use memoization to avoid recalculating the same subproblems:

class Solution:
    def solve(self, words, s):
        word_set = set(words)
        memo = {}
        
        def find_longest_chain(current_word):
            if current_word in memo:
                return memo[current_word]
            
            if current_word not in word_set:
                return 0
            
            max_length = 0
            for i in range(len(current_word)):
                new_word = current_word[:i] + current_word[i + 1:]
                max_length = max(max_length, find_longest_chain(new_word))
            
            memo[current_word] = 1 + max_length
            return memo[current_word]
        
        return find_longest_chain(s)

# Test the optimized solution
ob = Solution()
words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"]
s = "limit"
result = ob.solve(words, s)
print(f"Longest diminishing word chain length: {result}")

# Show the chain formation
print("\nChain formation:")
print("limit ? limi ? lii ? li")
Longest diminishing word chain length: 4

Chain formation:
limit ? limi ? lii ? li

Time Complexity

The time complexity is O(n × m × 2^m) where n is the number of words and m is the average word length. The optimized version with memoization reduces this significantly by avoiding redundant calculations.

Conclusion

This recursive approach efficiently finds the longest diminishing word chain by systematically removing characters and exploring all possible paths. The memoized version provides better performance for larger inputs.

Updated on: 2026-03-25T12:13:43+05:30

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements