Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2796: Repeat String

In this guide, we solve Leetcode #2796 Repeat String in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Easy
  • Premium: Yes
  • Tags: JavaScript

Intuition

We can repeat the string by building the output piece by piece.

Collecting the pieces in a list and joining once avoids quadratic concatenation costs.

Approach

Append the original string to a list times times, then join the list into the final string.

Steps:

  • Create an empty list.
  • Append s to it times times.
  • Join and return the result.

Example

Input: str = "hello", times = 2 Output: "hellohello" Explanation: "hello" is repeated 2 times

Python Solution

class Solution: def replicate(self, s: str, times: int) -> str: if times <= 0: return "" parts = [] for _ in range(times): parts.append(s) return "".join(parts)

Complexity

The time complexity is O(k)O(k)O(k) where kkk is the output length, and the space complexity is O(k)O(k)O(k).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy