Repeat String

Updated on 20 June, 2025
Repeat String header image

Problem Statement

The task is to enhance the functionality of all string data types in a programming environment such that we can append a new method string.replicate(x) to any string instance. This method should allow the string to be repeated x times without utilizing the built-in string.repeat method. It's important to ensure that the method behaves correctly based on the number of times the string needs to be replicated. The expected behavior is to return a new string that consists of the original string concatenated to itself x times. For example, the string "hello" when passed through the method with argument 3 should result in "hellohellohello".

Examples

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 <= 105
  • 1 <= str.length <= 1000

Approach and Intuition

The core of solving this problem involves creating a function that can emulate the behavior of repeating a string multiple times and then attaching this function as a method to the String prototype (or equivalent). Here's how you can approach this problem:

  1. Define a function, say replicate, which takes an integer x as an argument. This function will handle the logic of repeating the string x times.

  2. Inside the function, initialize an empty string that will store the final result.

  3. Use a loop to concatenate the original string to the result string for x times.

  4. After the loop finishes, return the accumulated result string.

  5. Attach this replicate function to the string's prototype. This allows all string instances to access this method natively as if it was a part of the built-in string methods.

  • For example in JavaScript, you would add it like:
    javascript
    String.prototype.replicate = function(x) {
        let result = "";
        for (let i = 0; i < x; i++) {
            result += this;
        }
        return result;
    };
    

This approach ensures that we stay within the given constraints and the behavior of string repetition aligns with the examples provided, efficiently handling strings up till 1000 characters and capable of repeating them up to 10^5 times. By iterating over the loop and manually adding the string to a result container, we satisfy the requirement of not using the native string.repeat method.

Solutions

  • JavaScript
js
String.prototype.duplicate = function(repeatCount) {
    const output = [];
    for (let index = 0; index < repeatCount; index++) {
        output.push(this);
    }

    return output.join('');
};

This JavaScript solution involves extending the String prototype to include a duplicate method that repeats the string for a specified number of times. Review the key components of the implementation:

  • Introduce a new method duplicate to the String prototype. This makes the method available to all string objects in your code.
  • Define repeatCount as a parameter, which determines the number of times the original string should be repeated.
  • Utilize an array, output, to hold the repeated string segments during each iteration of the loop.
  • Loop from 0 to repeatCount - 1, appending the string (this refers to the original string object on which the method is called) to the output array.
  • After completing the loop, merge all elements of the output array into a single string using the join('') method, which concatenates all array elements without any separator.

Apply this function simply by calling .duplicate(n) on any string variable, where n is the number of repetitions desired. For example, "hello".duplicate(3) would yield "hellohellohello". This method efficiently handles string repetition by leveraging array manipulations and could be directly used in any segments of your code requiring repeated string patterns.

Comments

No comments yet.