Consecutive Characters - Problem

The power of a string is defined as the maximum length of a non-empty substring that contains only one unique character.

Given a string s, return the power of s.

For example, if s = "leetcode", the power is 2 because the substring "ee" has length 2 and contains only the character 'e'.

Input & Output

Example 1 — Basic Case
$ Input: s = "leetcode"
Output: 2
💡 Note: The substring "ee" has length 2 with all characters identical. This is the longest such substring.
Example 2 — Single Character
$ Input: s = "abbcccddddeeeeedcba"
Output: 5
💡 Note: The substring "eeeee" has length 5 with all characters identical.
Example 3 — All Same
$ Input: s = "aaaa"
Output: 4
💡 Note: The entire string consists of identical characters, so the power is the length of the string.

Constraints

  • 1 ≤ s.length ≤ 500
  • s consists of only lowercase English letters.

Visualization

Tap to expand
Consecutive Characters: Find Maximum PowerInput: "leetcode"leetcodeLongest consecutive: "ee"Length = 2Scan string to find maximum consecutive identical charactersOutput: 2The power of the string is the length of the longest substring with identical characters
Understanding the Visualization
1
Input
String with various characters
2
Process
Find longest substring of identical characters
3
Output
Return the maximum length found
Key Takeaway
🎯 Key Insight: Use a single pass with running counter - reset when character changes, track maximum seen
Asked in
Google 15 Facebook 12 Amazon 8
28.5K Views
Medium Frequency
~15 min Avg. Time
980 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