Classic problem #1446EasyString
Consecutive Characters
Real-world version: The Longest Run of One MarkTracked in this browser only
Read Consecutive Characters on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A tape of lowercase letters reads tape.
Return the length of the longest stretch of neighbouring positions all holding the same letter.
Examples
Example 1
- Input
- tape = "aabbbcc"
- Output
- 3
The three b's in the middle form the longest stretch; the a's and the c's manage only two apiece.
Example 2
- Input
- tape = "zzzz"
- Output
- 4
The whole tape is one letter repeated, so the stretch runs the full length.
Example 3
- Input
- tape = "abc"
- Output
- 1
No two neighbouring letters match, so every stretch holds a single letter.
Constraints
- 1 <= tape.length <= 500
- The tape is made of lowercase English letters.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def max_power(tape: str) -> int:Java
public int maxPower(String tape)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's