← All posts

Leetcode Question 412: Fizz Buzz - My Simple Solution

1 min read

SeriesLeetCodepart 3 of 10
  1. LeetCode Problem 1480: Sum
  2. LeetCode Problem 1672: Solving the Richest Customer Wealth Challenge
  3. Leetcode Question 412: Fizz Buzz - My Simple Solution
  4. LeetCode Question 1342: Number of Steps to Reduce a Number to Zero - My Simple Solution
  5. Solving LeetCode Problem 876: Middle of the Linked List
  6. LeetCode Question 383: Ransom Note Solution Breakdown
  7. LeetCode 977: Squares of a Sorted Array Solution
  8. LeetCode 1295: Find Numbers with Even Number of Digits
  9. LeetCode 485 - Finding Max Consecutive Ones
  10. Is Bitwise & Better than Arithmetic to Determine Even or Odd? Let’s Dive In!
On this page

The Fizz Buzz Problem

Here’s the deal: you get a number n, and your task is to print an array with the numbers from 1 to n, but instead of multiples of 3, you do “Fizz”, multiples of 5 become “Buzz”, and for numbers that are multiples of both 3 and 5, you print “FizzBuzz”. Pretty straightforward, right? Let’s break it down.

Here’s the full problem statement if you want to check it out:

  • Write a program that outputs the string representation of numbers from 1 to n.
  • But for multiples of three it should output “Fizz” instead of the number.
  • For the multiples of five output “Buzz”.
  • For numbers which are multiples of both three and five output “FizzBuzz”.

My Fizz Buzz Solution

Here’s how I whipped this up in Java:

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> answer = new ArrayList<>();
        for(int i = 1; i <= n; i++) {
            String ans = "";
            if(i % 3 == 0) ans += "Fizz";
            if(i % 5 == 0) ans += "Buzz";
            if(ans.isEmpty()) ans = String.valueOf(i);
            answer.add(ans);
        }
        return answer;
    }
}

Complexity Analysis

Now, let’s chat about performance because, let’s face it, nobody likes shit code that runs like a snail.

  • Time Complexity: This bad boy runs in O(n) time because we’re looping through numbers from 1 to n. Each iteration checks conditions and does basic string operations, so there are no hidden surprises here.

  • Space Complexity: The space complexity is O(n) as well because we’re storing up to n strings in our list. That’s pretty standard for problems that require returning collections.

References:

And that’s a wrap, folks! Happy coding, and remember: keep it funky.