Watch the video solution for Repeat String, a easy level problem. This walkthrough by AlgorithmicIQ has 17 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Write code that enhances all strings such that you can call the string.replicate(x) method on any string and it will return repeated string x times.
Try to implement it without using the built-in method string.repeat.
Example 1:
Input: str = "hello", times = 2 Output: "hellohello" Explanation: "hello" is repeated 2 times
Example 2:
Input: str = "code", times = 3 Output: "codecodecode" Explanation: "code" is repeated 3 times
Example 3:
Input: str = "js", times = 1 Output: "js" Explanation: "js" is repeated 1 time
Constraints:
1 <= times <= 1051 <= str.length <= 1000Follow up: Let's assume, for the sake of simplifying analysis, that concatenating strings is a constant time operation
O(1). With this assumption in mind, can you write an algorithm with a runtime complexity of O(log n)?Problem Overview: You are given a base string s and an integer k. The task is to construct a new string formed by repeating s exactly k times. For example, if s = "ab" and k = 3, the result should be "ababab".
Approach 1: Iterative Concatenation (O(n * k) time, O(n * k) space)
The straightforward way is to build the result string step by step. Start with an empty string and iterate k times. During each iteration, append the original string s to the result. Each append copies characters from s, so if s has length n, the total work becomes n * k. The final string also stores n * k characters, which leads to O(n * k) space usage. This method uses basic string manipulation and a simple loop, making it easy to implement and understand.
Approach 2: Built-in Repeat Method (O(n * k) time, O(n * k) space)
Most languages provide a built-in utility to repeat a string multiple times. In JavaScript and TypeScript, the String.prototype.repeat() method handles this directly. Calling s.repeat(k) internally allocates a new string and copies the characters of s k times. The algorithmic complexity remains O(n * k) because every character still needs to be written to the final output. The benefit is cleaner code and fewer chances for off-by-one mistakes. This approach relies purely on string operations and avoids manual loops.
Although the logic is simple, understanding the cost of string construction is useful. Each repetition requires copying characters into the result buffer. Languages with immutable strings allocate new memory for the final result, so the total memory footprint grows linearly with the output length.
Recommended for interviews: The iterative approach demonstrates that you understand how string construction works internally. However, interviewers typically expect you to use the built-in repeat() function when available because it expresses intent clearly and keeps the implementation concise. Both approaches rely on fundamental string manipulation concepts and run in linear time relative to the size of the produced string.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Concatenation | O(n * k) | O(n * k) | When implementing manually or when built-in repeat utilities are unavailable |
| Built-in repeat() Method | O(n * k) | O(n * k) | Preferred in JavaScript/TypeScript for concise and readable implementation |