#2259
Easy Algorithms Remove digit from number to maximize result
String Greedy Enumeration
48.2% acceptance
Feb 25, 2026
948
66
You are given a string number representing a positive integer and a character digit.
Return the resulting string after removing exactly one occurrence of digit from number such that the value of the resulting string in decimal form is maximized. The test cases are generated such that digit occurs at least once in number.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn remove_digit(number: String, digit: char) -> String {
let bytes = number.as_bytes();
let d = digit as u8;
let n = bytes.len();
let mut best = String::new();
for i in 0..n {
if bytes[i] == d {
let mut s = String::with_capacity(n - 1);
s.push_str(&number[..i]);
s.push_str(&number[i+1..]);
if best.is_empty() || s > best {
best = s;
}
}
}
best
}
}