Skip to main content
Back to problems
#2796
Easy JavaScript

Repeat string

93.1% acceptance
Mar 31, 2026
25
2
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.

Solution

TypeScript
Time O(n)
Space O(1)
LeetCode
solution.ts
interface String {
  replicate(times: number): string;
}

String.prototype.replicate = function (times): string {
  let result = "";
  for (let i = 0; i < times; i++) {
  result += this;
  }
  return result;
};