Program to find length of substring with consecutive common characters in Python

Finding the length of the longest substring with consecutive identical characters is a common string processing problem. We need to iterate through the string and track the maximum count of consecutive characters.

Problem Statement

Given a string, find the length of the longest substring containing the same consecutive characters. For example, in the string "abbbaccabbbba", the longest substring with consecutive identical characters is "bbbb" with length 4.

Algorithm

The approach involves iterating through the string and maintaining two counters ?

  • Track the current consecutive character count
  • Track the maximum consecutive count seen so far
  • Compare adjacent characters and update counters accordingly
  • Return the maximum count found

Implementation

def find_longest_consecutive(s):
    if len(s) == 0:
        return 0
    
    max_count = 1
    current_count = 1
    
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            current_count += 1
        else:
            max_count = max(current_count, max_count)
            current_count = 1
    
    # Check one final time after loop ends
    max_count = max(current_count, max_count)
    return max_count

# Test the function
test_string = "abbbaccabbbba"
result = find_longest_consecutive(test_string)
print(f"Input: {test_string}")
print(f"Length of longest consecutive substring: {result}")
Input: abbbaccabbbba
Length of longest consecutive substring: 4

Alternative Implementation with Class

Here's the class-based approach similar to the original ?

class Solution:
    def solve(self, s):
        if len(s) == 0:
            return 0
        
        max_length = 1
        current_length = 1
        
        for i in range(1, len(s)):
            if s[i] == s[i-1]:
                current_length += 1
            else:
                max_length = max(current_length, max_length)
                current_length = 1
        
        return max(current_length, max_length)

# Test the solution
solution = Solution()
test_cases = ["abbbaccabbbba", "aaaa", "abcdef", "aabbcc", ""]

for test in test_cases:
    result = solution.solve(test)
    print(f"Input: '{test}' ? Output: {result}")
Input: 'abbbaccabbbba' ? Output: 4
Input: 'aaaa' ? Output: 4
Input: 'abcdef' ? Output: 1
Input: 'aabbcc' ? Output: 2
Input: '' ? Output: 0

How It Works

The algorithm compares each character with the previous one. When characters match, we increment the current count. When they don't match, we update the maximum count and reset the current count to 1. We also check the final count after the loop ends to handle cases where the longest sequence is at the end of the string.

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the string
  • Space Complexity: O(1), as we only use a constant amount of extra space

Conclusion

This solution efficiently finds the longest consecutive character substring using a single pass through the string. The key insight is maintaining both current and maximum counts while comparing adjacent characters.

Updated on: 2026-03-25T10:51:23+05:30

589 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements