#3773
Medium Algorithms Maximum number of equal length runs
Hash Table String Counting
84.3% acceptance
Mar 31, 2026
3
3
You are given a string s consisting of lowercase English letters.
A run in s is a substring of equal letters that cannot be extended further. For example, the runs in "hello" are "h", "e", "ll", and "o".
You can select runs that have the same length in s.
Return an integer denoting the maximum number of runs you can select in s.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn max_same_length_runs(s: String) -> i32 {
let b = s.as_bytes();
let mut counts = std::collections::HashMap::new();
let mut i = 0;
while i < b.len() {
let c = b[i];
let mut j = i;
while j < b.len() && b[j] == c {
j += 1;
}
*counts.entry(j - i).or_insert(0) += 1;
i = j;
}
*counts.values().max().unwrap()
}
}