#2264
Easy Algorithms Largest 3 same digit number in string
String
72.7% acceptance
Feb 25, 2026
1388
55
You are given a string num representing a large integer. An integer is good if it meets the following conditions:
It is a substring of num with length 3.
It consists of only one unique digit.
Return the maximum good integer as a string or an empty string "" if no such integer exists.
Note:
A substring is a contiguous sequence of characters within a string.
There may be leading zeroes in num or a good integer.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn largest_good_integer(num: String) -> String {
let bytes = num.as_bytes();
let mut best: Option<u8> = None;
for i in 0..bytes.len()-2 {
if bytes[i] == bytes[i+1] && bytes[i+1] == bytes[i+2] {
let d = bytes[i];
if best.map_or(true, |b| d > b) {
best = Some(d);
}
}
}
best.map_or(String::new(), |d| {
let ch = d as char;
format!("{}{}{}", ch, ch, ch)
})
}
}