Substring in infinitely extended string in JavaScript

We are required to write a JavaScript function that takes in a string of characters as the first argument and a start index and end index as second and third argument respectively. The function should find, had that string, provided as the first argument, been extended forever by appending the same string at end each time, what would have been the substring encapsulated by the start index and the end index.

For example, if we have the string 'helloo' repeated infinitely like 'hellooheloohelloo...', we can extract any substring using start and end indices.

Understanding the Problem

If the input string and the indices are:

const str = 'helloo';
const start = 11;
const end = 15;

The infinitely extended string would look like: 'helooheloohelloo...'. Characters at indices 11-14 would be 'hel'.

Then the output should be:

const output = 'hel';

Solution Approach

The key insight is to use the modulo operator to map any index back to its position within the original string's length. We calculate how many complete repetitions occur before our start index and handle the substring extraction accordingly.

Example

Following is the complete solution:

const str = 'helloo';
const start = 12;
const end = 15;

const findSubstring = (str = '', start, end) => {
    let n = str.length;
    let result = '';
    
    // Extract characters one by one from start to end-1
    for (let i = start; i < end; i++) {
        let index = i % n;  // Map to original string position
        result += str[index];
    }
    
    return result;
};

console.log(findSubstring(str, start, end));

Output

hel

How It Works

The algorithm works by:

  • Using modulo operation i % n to find the corresponding character in the original string
  • Iterating from start index to end index (exclusive)
  • Building the result string character by character

Alternative Optimized Solution

Here's a more efficient approach that handles complete string repetitions:

const findSubstringOptimized = (str = '', start, end) => {
    let n = str.length;
    let length = end - start;
    let result = '';
    
    let startPos = start % n;
    let completeReps = Math.floor(length / n);
    let remainder = length % n;
    
    // Handle the partial string at the beginning
    if (startPos + remainder <= n) {
        result = str.substring(startPos, startPos + remainder);
    } else {
        result = str.substring(startPos) + str.substring(0, remainder - (n - startPos));
    }
    
    // Add complete repetitions if needed
    result = str.repeat(completeReps) + result;
    
    return result;
};

console.log(findSubstringOptimized('helloo', 12, 15));
console.log(findSubstringOptimized('abc', 5, 11));

Output

hel
abcabc

Conclusion

The modulo operator is the key to solving substring problems in infinitely extended strings. The simple character-by-character approach works well for small ranges, while the optimized version handles larger ranges more efficiently.

Updated on: 2026-03-15T23:19:00+05:30

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements