Consecutive Characters - Problem

Think of a string as a sequence of characters where certain letters might repeat consecutively, like in "aaabbc". The power of a string is defined as the maximum length of any consecutive sequence of identical characters.

For example, in the string "leetcode", the letter 'e' appears consecutively twice, giving it a power of 2. In "abbcccddddeeeeedcba", the letter 'e' appears 5 times in a row, making the power 5.

Your task: Given a string s, find and return its power - the length of the longest substring containing only one unique repeating character.

Input & Output

example_1.py โ€” Basic consecutive characters
$ Input: s = "leetcode"
โ€บ Output: 2
๐Ÿ’ก Note: The substring "ee" has the power of 2, which is the maximum among all substrings.
example_2.py โ€” Multiple consecutive groups
$ Input: s = "abbcccddddeeeeedcba"
โ€บ Output: 5
๐Ÿ’ก Note: The substring "eeeee" has the power of 5, which is longer than "dddd" (power 4) or "ccc" (power 3).
example_3.py โ€” Single character
$ Input: s = "a"
โ€บ Output: 1
๐Ÿ’ก Note: A single character has power 1, as it's the only character forming a consecutive sequence.

Visualization

Tap to expand
String Power VisualizationInput String: "abbcccddddeeeeedcba"apow:1bpow:2bcpow:3ccdpow:4dddepow:5eeeeAlgorithm TraceStep 1: Initialize maxPower = 1, currentPower = 1Step 2: Compare s[1]='b' with s[0]='a' โ†’ different, reset currentPower = 1Step 3: Compare s[2]='b' with s[1]='b' โ†’ same, currentPower = 2, maxPower = 2Step 4: Continue scanning... 'ccc' gives maxPower = 3, 'dddd' gives maxPower = 4Step 5: 'eeeee' sequence found โ†’ maxPower = 5 (final answer)Performance Analysisโšก Time Complexity: O(n) - single pass through string๐Ÿ’พ Space Complexity: O(1) - only using counter variables
Understanding the Visualization
1
Initialize tracking
Start with maxPower=1 and currentPower=1, since any single character has power 1
2
Scan left to right
Compare each character with its predecessor. If same, increment currentPower; if different, reset to 1
3
Update maximum
Whenever currentPower increases, check if it exceeds our recorded maxPower
4
Return result
After scanning the entire string, maxPower contains the length of the longest consecutive sequence
Key Takeaway
๐ŸŽฏ Key Insight: Consecutive identical characters form natural groups that can be counted efficiently in a single pass, eliminating the need for nested loops or additional data structures.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string of length n, checking each character exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space for counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of only lowercase English letters
  • The string is non-empty
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
67.2K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen