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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code