Talentd
FleetCode

1446. Consecutive Characters

Easy60.1% acceptance4 companies

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

Given a string s, return the power of s.

Example 1:

Input: s = "leetcode"
Output: 2
Explanation: The substring "ee" is of length 2 with the character 'e' only.

Example 2:

Input: s = "abbcccddddeeeeedcba"
Output: 5
Explanation: The substring "eeeee" is of length 5 with the character 'e' only.

Constraints:

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

Topics

Hints open one at a time

Hint 1
Keep an array power where power[i] is the maximum power of the i-th character.
Hint 2
The answer is max(power[i]).
9:05
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxPower(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"leetcode"
Input
"abbcccddddeeeeedcba"
Your code is saved on this device.Solution