#1323
Easy Algorithms Maximum 69 number
Math Greedy
84.5% acceptance
Feb 25, 2026
3307
240
You are given a positive integer num consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn maximum69_number(num: i32) -> i32 {
let s = num.to_string();
if let Some(pos) = s.find('6') {
let mut v: Vec<char> = s.chars().collect();
v[pos] = '9';
v.iter().collect::<String>().parse().unwrap()
} else {
num
}
}
}