Learnitweb

LeetCode Problem 1446 Consecutive Characters


1. Problem Summary

You are given a string s consisting only of lowercase English letters.
Your task is to determine the length of the longest substring made up of the same repeated character.

In other words, you must find the maximum count of consecutive repeating characters appearing contiguously in the string.

Example interpretation:
For input:

"abbcccddddeeeeedcba"

The longest run is "eeeee" with length 5, so the answer is 5.


2. Key Insights

A substring must be continuous

Only consecutive identical characters count.

We only need to track streak lengths

No need to store substrings or indices.

Single scan is enough

While traversing:

  • Compare character with previous
  • If same → increase streak
  • If different → reset streak
  • Track a global max streak length

Edge cases are simple

  • If string length is 1 → answer is 1
  • Characters can repeat multiple separate times
  • All characters could be the same

3. Approach

Linear scan with running streak counter

Steps:

  1. Initialize: maxCount = 1 currentCount = 1
  2. Loop from index 1 to end:
    • If s[i] == s[i-1]: currentCount++
    • Else: reset currentCount = 1
    • Update global maximum: maxCount = max(maxCount, currentCount)
  3. Return maxCount

This approach efficiently captures longest run.


4. Time and Space Complexity

Time Complexity: O(N)

Loops through the string once.

Space Complexity: O(1)

Uses only constant tracking variables.


5. Java Solution

class Solution {
    public int maxPower(String s) {
        int maxCount = 1;
        int currentCount = 1;

        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == s.charAt(i - 1)) {
                currentCount++;
            } else {
                currentCount = 1;
            }
            maxCount = Math.max(maxCount, currentCount);
        }

        return maxCount;
    }
}

6. Dry Run Examples

Example 1

Input:

"abbcccddddeeeeedcba"

Processing streaks:

a → length 1
bb → length 2
ccc → length 3
dddd → length 4
eeeee → length 5   (max)
d → reset to 1
c → reset to 1
b → reset to 1
a → reset to 1

Final output:

5

Example 2

Input:

"hooraaaaaaaaaaay"

Longest streak:

"aaaaaaaaaa" → length 10

Output:

10

Example 3

Input:

"cc"

Two identical consecutive characters → length 2

Output:

2

Example 4

Input:

"j"

Single character → length 1

Output:

1

Example 5

Input:

"trrrrreeeeeet"

Two longest streaks:
"rrrrrr" length 6
"eeeee" length 5
Answer = 6

Output:

6

7. Why This Solution Works

  • Consecutive measure requires local comparison
  • No sorting or frequency counting needed
  • Efficient single traversal
  • Works for any character patterns
  • Handles long identical runs naturally
  • Minimal logic and storage

8. Common Mistakes

  1. Counting total occurrences, not consecutive ones
  2. Resetting incorrectly on character change
  3. Forgetting to update max at each step
  4. Using substring extraction (unnecessary and costly)
  5. Not handling input length 1 properly