Skip to main content
Back to problems
#1446
Easy Algorithms

Consecutive characters

String
60.3% acceptance
Feb 25, 2026
1842
35
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_power(s: String) -> i32 {
    let bytes = s.as_bytes();
    let mut best = 1;
    let mut cur = 1;
    for i in 1..bytes.len() {
      if bytes[i] == bytes[i-1] { cur += 1; best = best.max(cur); }
      else { cur = 1; }
    }
    best
  }
}