
Problem Statement
Given a positive integer, n
, one must generate a string array that follows the FizzBuzz rule set. This rule set requires:
- Each position
i
in the array, wherei
is a multiple of both 3 and 5, should hold the value "FizzBuzz". - For indices that are only multiples of 3, the value should be "Fizz".
- For indices that are only multiples of 5, the value should be "Buzz".
- If an index doesn’t meet any of the above conditions, it should simply contain its numeric value as a string.
The resulting list should be 1-indexed, meaning the array starts at index 1 rather than the conventional index 0 in programming.
Examples
Example 1
Input:
n = 3
Output:
["1","2","Fizz"]
Example 2
Input:
n = 5
Output:
["1","2","Fizz","4","Buzz"]
Example 3
Input:
n = 15
Output:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints
1 <= n <= 104
Approach and Intuition
The problem of generating a FizzBuzz sequence is a well-structured and straightforward algorithmic task. Here is the approach based on the given constraints and examples:
- Initialize an empty list called
answer
which will hold the result strings. - Loop through numbers from 1 to
n
. - For each number:
- Check if the number is divisible by both 3 and 5:
- If true, append "FizzBuzz" to the
answer
list.
- If true, append "FizzBuzz" to the
- Else, check if the number is divisible by 3:
- If true, append "Fizz".
- Else, check if the number is divisible by 5:
- If true, append "Buzz".
- If none of the conditions apply, just append the number itself as a string.
- Check if the number is divisible by both 3 and 5:
- Once all numbers have been evaluated and the corresponding results appended, return the
answer
list.
The given examples perfectly illustrate these conditional paths:
- For
n = 3
, we add "1", "2", "Fizz" to the list as neither 1 nor 2 is divisible by 3 or 5, and 3 is only divisible by 3. - For
n = 5
, the progression adds "Buzz" at the 5th position since it is divisible by 5. - For
n = 15
, all conditions reflect periodically, and "FizzBuzz" is added at the 15th position as it meets the divisibility condition for both 3 and 5.
Through merging conditional checks and string operations, this process constructs the FizzBuzz sequence efficiently within the constraints that 1 <= n <= 104
, thereby providing a solution applicable even at the upper limit of typical small to medium range inputs.
Solutions
- Java
- Python
class Solution {
public List<String> generateFizzBuzz(int maxNumber) {
List<String> result = new ArrayList<String>();
Map<Integer, String> fbMappings = new HashMap<Integer, String>() {
{
put(3, "Fizz");
put(5, "Buzz");
}
};
List<Integer> keys = new ArrayList<>(Arrays.asList(3, 5));
for (int i = 1; i <= maxNumber; i++) {
String output = "";
for (Integer divisor: keys) {
if (i % divisor == 0) {
output += fbMappings.get(divisor);
}
}
if (output.isEmpty()) {
output += String.valueOf(i);
}
result.add(output);
}
return result;
}
}
This Java solution implements the Fizz Buzz problem where it prints "Fizz" for numbers divisible by 3, "Buzz" for numbers divisible by 5, and "FizzBuzz" for numbers divisible by both 3 and 5. For numbers not divisible by 3 or 5, it prints the number itself. The program defines a generateFizzBuzz
method, which:
- Initializes a list to hold results and a dictionary to map integers to their corresponding "Fizz" or "Buzz".
- Iterates through numbers from 1 to the specified
maxNumber
. - For each number, checks divisibility using the predefined mappings. If divisible, it appends "Fizz", "Buzz", or both to an output string based on the conditions.
- If the number is not divisible by 3 or 5, the number itself is converted to a string and added to results.
- The method returns the list containing the Fizz Buzz sequence for the range from 1 to
maxNumber
.
Use this code by instantiating the Solution
class and calling the generateFizzBuzz
method, passing the upper limit as an argument, to generate the desired sequence.
class Solution:
def fizzBuzz(self, limit: int) -> List[str]:
result = []
fizzbuzz_mapping = {3: "Fizz", 5: "Buzz"}
keys_list = [3, 5]
for i in range(1, limit + 1):
temp_str = []
for divisor in keys_list:
if i % divisor == 0:
temp_str.append(fizzbuzz_mapping[divisor])
if not temp_str:
temp_str.append(str(i))
result.append(''.join(temp_str))
return result
The given solution in Python implements the "Fizz Buzz" problem, which is a well-known test often used in programming interviews. The task involves printing the numbers from 1 to a specified limit, but for multiples of three, print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".
- The code defines a class
Solution
with a methodfizzBuzz
that accepts an integerlimit
. - Inside this method, an empty list
result
is initialized to store the outputs for each number up to the limit. - A dictionary
fizzbuzz_mapping
is used to map integers 3 and 5 to strings "Fizz" and "Buzz" respectively. - It iterates through each number from 1 to
limit
(inclusive). - For each number, it initializes an empty list
temp_str
. - It then iterates over the keys list
[3, 5]
. If the number is divisible by a key, the corresponding string fromfizzbuzz_mapping
is appended totemp_str
. - If
temp_str
is still empty (meaning the number was not divisible by either 3 or 5), the number itself is converted to a string and added totemp_str
. - The results are then joined without spaces and appended to the
result
list. - Finally, the list
result
containing the "Fizz", "Buzz", "FizzBuzz", or number values is returned.
No comments yet.