Program to find length longest prefix sequence of a word array in Python

Suppose we have a list of words called w, with lowercase strings. We have to find the length of the longest sequence of w where each previous word is the prefix of the next word and the next word has just one new character appended.

So, if the input is like w = ["pqr", "pq", "m", "mn", "pqrs"], then the output will be 3 because we can get the sequence: ["pq", "pqr", "pqrs"], whose length is 3.

Algorithm

To solve this, we will follow these steps ?

  • Sort the list w
  • Create a dictionary dp where default value for a key is 0
  • Initialize res to 0
  • For each word in w, do:
    • Set dp[word] = dp[substring of word up to second last element] + 1
    • Update res = maximum of res and dp[word]
  • Return res

Example

Let us see the following implementation to get better understanding ?

from collections import defaultdict

def solve(w):
    w.sort()
    dp = defaultdict(int)
    res = 0
    for word in w:
        dp[word] = dp[word[:-1]] + 1
        res = max(res, dp[word])
    return res

w = ["pqr", "pq", "m", "mn", "pqrs"]
print(solve(w))

Output

3

How It Works

The algorithm uses dynamic programming with the following logic:

  • Sorting: Ensures shorter words come before longer ones
  • DP table: dp[word] stores the length of longest sequence ending with that word
  • Prefix check: word[:-1] gives the prefix by removing the last character
  • Building sequences: Each word extends its prefix's sequence by 1

Step-by-Step Trace

from collections import defaultdict

def solve_with_trace(w):
    w.sort()
    print(f"Sorted words: {w}")
    
    dp = defaultdict(int)
    res = 0
    
    for word in w:
        prefix = word[:-1]
        dp[word] = dp[prefix] + 1
        res = max(res, dp[word])
        print(f"Word: '{word}', Prefix: '{prefix}', Length: {dp[word]}")
    
    return res

w = ["pqr", "pq", "m", "mn", "pqrs"]
result = solve_with_trace(w)
print(f"Longest sequence length: {result}")
Sorted words: ['m', 'mn', 'pq', 'pqr', 'pqrs']
Word: 'm', Prefix: '', Length: 1
Word: 'mn', Prefix: 'm', Length: 2
Word: 'pq', Prefix: 'p', Length: 1
Word: 'pqr', Prefix: 'pq', Length: 2
Word: 'pqrs', Prefix: 'pqr', Length: 3
Longest sequence length: 3

Conclusion

This dynamic programming solution efficiently finds the longest prefix sequence by sorting words and tracking sequence lengths. The time complexity is O(n log n) due to sorting, where n is the number of words.

Updated on: 2026-03-26T17:38:53+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements