easy +8 pts

Repeat each character n times

Transform a string by repeating each character a given number of times.

Write a function `repeat_chars(s: str, n: int) -> str` that returns a new string where each character from the original string `s` appears `n` times consecutively, preserving the original order. For example, if `s = "ab"` and `n = 3`, the result should be `"aaabbb"`. If `n` is 0 or negative, return an empty string. The input string may contain spaces, digits, punctuation, or any Unicode character.

Constraints

- `s` is a string (any length, including empty). - `n` is an integer (can be positive, zero, or negative). - Time complexity: O(len(s) * max(n, 0)). - Space complexity: O(len(s) * max(n, 0)).

Example

>>> repeat_chars("ab", 3)
'aaabbb'
>>> repeat_chars("hello", 2)
'hheelllloo'
>>> repeat_chars("abc", 0)
''
>>> repeat_chars("", 5)
''
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about how to repeat a single character in Python.
You can use a loop or a comprehension to build the result.
Remember to handle n <= 0 by returning an empty string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.