#412
Easy Algorithms Fizz buzz
Math String Simulation
75.3% acceptance
Jan 13, 2026
3340
458
Given an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn fizz_buzz(n: i32) -> Vec<String> {
(1..=n)
.map(|i| {
if i % 15 == 0 {
"FizzBuzz".to_string()
} else if i % 3 == 0 {
"Fizz".to_string()
} else if i % 5 == 0 {
"Buzz".to_string()
} else {
i.to_string()
}
})
.collect()
}
}