Program to find length of longest contiguous sublist with same first letter words in Python

Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where each word has the same first letter.

So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, because the longest contiguous sublist is ["she", "sells", "seashells"] where each word starts with 's'.

Algorithm

To solve this, we will follow these steps ?

  • Initialize cnt = 1 to track current sequence length

  • Initialize maxcnt = 0 to track maximum length found

  • Initialize prev_char as empty string to store previous first letter

  • For each word in words, do

    • If prev_char is empty, set prev_char to first letter of current word

    • If prev_char matches first letter of current word, increment cnt

    • Otherwise, reset prev_char to first letter of current word and cnt to 1

    • Update maxcnt with maximum of maxcnt and cnt

  • Return maxcnt

Example

Let us see the following implementation to get better understanding ?

def solve(words):
    cnt = 1
    maxcnt = 0
    prev_char = ""
    
    for word in words:
        if prev_char == "":
            prev_char = word[0]
        elif prev_char == word[0]:
            cnt += 1
        else:
            prev_char = word[0]
            cnt = 1
        maxcnt = max(maxcnt, cnt)
    
    return maxcnt

words = ["she", "sells", "seashells", "on", "the", "sea", "shore"]
print(solve(words))

The output of the above code is ?

3

How It Works

The algorithm processes each word sequentially:

  • ["she"] ? First word, prev_char = 's', cnt = 1, maxcnt = 1

  • ["she", "sells"] ? Same first letter 's', cnt = 2, maxcnt = 2

  • ["she", "sells", "seashells"] ? Same first letter 's', cnt = 3, maxcnt = 3

  • ["on"] ? Different first letter 'o', cnt = 1, maxcnt remains 3

  • ["the"] ? Different first letter 't', cnt = 1, maxcnt remains 3

  • ["sea"] ? Different first letter 's', cnt = 1, maxcnt remains 3

  • ["shore"] ? Same first letter 's', cnt = 2, maxcnt remains 3

Alternative Approach

Here's a more concise implementation using a single pass ?

def solve_alternative(words):
    if not words:
        return 0
    
    max_length = 1
    current_length = 1
    
    for i in range(1, len(words)):
        if words[i][0] == words[i-1][0]:
            current_length += 1
        else:
            max_length = max(max_length, current_length)
            current_length = 1
    
    return max(max_length, current_length)

words = ["she", "sells", "seashells", "on", "the", "sea", "shore"]
print(solve_alternative(words))

The output of the above code is ?

3

Conclusion

Both approaches solve the problem in O(n) time complexity by tracking the current sequence length and updating the maximum length found. The alternative approach is slightly more efficient as it compares adjacent words directly.

Updated on: 2026-03-26T15:17:54+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements