[Python] easy solution, explained
This is very classical and old interview question and I think already a lot was discussed. Here is my solution, where we use simple approach: we iterate over nu
This is very classical and old interview question and I think already a lot was discussed. Here is my solution, where we use simple approach: we iterate over numbers, check for each number what we need to add and add it. Also, for more universality I use `words = ["Fizz", "Buzz"]`, `mods = [3,5]` and `k=2`. What we do next is just iterate over numbers and for each number check if it is divisible by `3` and by `5`.\n\n**Complexity**: time complexity is `O(n)`, more precisely, we do `2` division checks for each number and then add result to `res`. It can be optimized, if we do not check each number, but firstly create array `[1,2,3,...,n]`, and then traverse only numbers divisible by `3` in one pass and numbers divisible by `5` in second pass. Space complexity is `O(n)` as well.\n\n```\nclass Solution:\n    def fizzBuzz(self, n):\n        words, mods = ["Fizz", "Buzz"], [3, 5]\n        k, res = 2, []\n        for i in range(1, n+1):\n            current_str = ""\n            for j in range(k):\n                if i%mods[j] == 0: current_str += words[j]\n                    \n            if not current_str:\n                res.append(str(i))\n            else:\n                res.append(current_str)\n                \n        return res\n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
[]