DEV Community

Cover image for Merge Strings Alternately
Luqman Shaban
Luqman Shaban

Posted on

Merge Strings Alternately

Hey there! Learning data structures and implementing the right algorithms in your software has proven to increase the sufficiency, speed and performance of your program. I have decided to document how I tackled and solved the challenges from leetcode. In this series, we'll delve into the LeetCode 75 a good starting point to practice your data structure and algorithm skills.

In this article, will begin our journey with the first challenge: Merging Strings Alternately. Here's the problem:

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.

Let's break this down to further understand the challenge at hand. We are given two strings word1 and word2. The strings should be merged in an alternative sequence. For instance, let's assume word1 and word2 have the values abc and def respectively. After execution of the code, the initial value of the merged string should be adbecf.
If word1 is longer than word2 or vice versa, the remaining string should be appended at the end of the merged string. i.e abc and defgh should return adbecfgh.

I'll be using TypeScript to grapple with this challenge. You don't need to know TypeScript, as should be able to follow along with any programming language you're comfortable with. Here's the starter code:


function mergeAlternately(word1: string, word2: string): string {

};
Enter fullscreen mode Exit fullscreen mode

We are given a function with two string parameters. The function should return a merged string of the two arguments passed to the function in an alternating order.
We can initialize an empty array and name it mergedString.

function mergeAlternately(word1: string, word2: string): string {
    let mergedString: string[] = []
};
Enter fullscreen mode Exit fullscreen mode

We can now create two variables that will represent the length of the parameters word1 and word2.


function mergeAlternately(word1: string, word2: string): string {
    let mergedString: string[] = [];
    let i: number = 0;
    let j: number = 0;
};
Enter fullscreen mode Exit fullscreen mode

We can now use loops to handle this task. The loop should iterate over the two arguments passed to the function and on each iteration, the value of the current index of the variables i and j, will be pushed to the mergedString array.
Let's see how we can do this using a while Loop.

function mergeAlternately(word1: string, word2: string): string {
    let mergedString: string[] = [];
    let i: number = 0;
    let j: number = 0;

    while(i < word1.length && j < word2.length) {
        mergedString.push(word1[i]); // you can directly increment word[i++]
        mergedString.push(word2[j]); // you can directly increment word[i++]
        i++; 
        j++;
    }
};
Enter fullscreen mode Exit fullscreen mode

During a scenario where one parameter is longer than the other, the remaining strings should be merged at the end. So we'll add two more loops, while the first loop will merge the remaining strings of word1 to mergedString the second loop will merge the remaining strings of word2.

function mergeAlternately(word1: string, word2: string): string {
    let mergedString: string[] = [];
    let i: number = 0;
    let j: number = 0;

    while(i < word1.length && j < word2.length) {
        mergedString.push(word1[i]); // you can directly increment word[i++]
        mergedString.push(word2[j]); // you can directly increment word[i++]
        i++; 
        j++;
    }

    while(i < word1.length) {
       mergedString.push(word1[i++]); 
       //  mergedString.push(word1[i]);
       // i ++;
    }

    while(j < word2.length) {
       mergedString.push(word2[j++]); 
       //  mergedString.push(word2[j]);
       // j ++;
    }
};
Enter fullscreen mode Exit fullscreen mode

The final step will be to convert the array into a string since the function is required to return a string. We'll use the join method to achieve this.

function mergeAlternately(word1: string, word2: string): string {
    let mergedString: string[] = [];
    let i: number = 0;
    let j: number = 0;

    while(i < word1.length && j < word2.length) {
        mergedString.push(word1[i]); // you can directly increment word[i++]
        mergedString.push(word2[j]); // you can directly increment word[i++]
        i++; 
        j++;
    }

    while(i < word1.length) {
       mergedString.push(word1[i++]); 
       //  mergedString.push(word1[i]);
       // i ++;
    }

    while(j < word2.length) {
       mergedString.push(word2[j++]); 
       //  mergedString.push(word2[j]);
       // j ++;
    }

    return mergedString.join('');
};
Enter fullscreen mode Exit fullscreen mode

And that's it!! If you console.log the function and pass to strings as arguments it should return a merged string.
GitHub Repo

I appreciate your time spent reading this article, if there are any errors in the code or you have a better solution please share down below in the comments section. See you in the next article.

LinkedIn
Twitter
GitHub

Top comments (0)