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