Merge Strings Alternately

Luqman ShabanLuqman Shaban
4 min read

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 {

};

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[] = []
};

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;
};

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++;
    }
};

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 ++;
    }
};

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('');
};

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

0
Subscribe to my newsletter

Read articles from Luqman Shaban directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Luqman Shaban
Luqman Shaban

About I am a software developer with a passion for creating innovative and user-friendly applications. I am currently enrolled in a Diploma in Software Development at the Institute of Software Technology, and I have also completed the Meta Front End Development Course. I am proficient in a variety of programming languages and technologies, including HTML, CSS, JavaScript, React, Node.js, Express, Sequelize, and MySQL. I am also a quick learner and have strong communication skills. I am passionate about software development because I enjoy the challenge of solving problems and creating solutions that make people's lives easier. I am also motivated by the opportunity to work on innovative projects and contribute to the development of new technologies. If you are looking for a talented and dedicated software developer, I would be happy to discuss how I can contribute to your team. Please connect with me on LinkedIn or visit my portfolio to learn more about my work.