LeetCode 1768 - From Initial Solution to Optimization.

LeetCode 1768 - From Initial Solution to Optimization.

Solving LeetCode 1768: An Optimization Journey

ยท

3 min read

Introduction

LeetCode 1768 asks us to merge two strings alternately. In this blog post, I would like to take you through my journey of solving this Leetcode problem. We will begin with my initial solution and then explore how I optimized it to achieve better performance and readability. ๐Ÿš€

Initial Solution

// Initial code
var mergeAlternately = function(word1, word2) {
    const arr1 = Array.from(word1);
    const arr2 = Array.from(word2);
    const result = [];
    let count = 0;

    while (arr1.length > 0 && arr2.length > 0) {
        if (count % 2 === 0) {
            result.push(arr1.shift());
        } else {
            result.push(arr2.shift());
        }
        count++;
    }

    while (arr1.length) {
        result.push(arr1.shift());
    }

    while (arr2.length) {
        result.push(arr2.shift());
    }

    return result.join("");
};

When I initially tackled this problem, I started by converting the input strings into arrays, thinking it would make manipulation easier. I used a while loop to iterate through both arrays, alternately pushing characters from each array into the result array based on the count variable(I should name it flag though). When one of the arrays became empty, I used two additional while loops to process any remaining characters.

While this initial solution worked, it had plenty of room for improvement. The use of shift on arrays(O(n)) and the count variable made the code less efficient and harder to read.

Optimized Solution

// Optimized code with a time complexity of O(a + b) and space complexity of O(a + b)
// Optimization Points:
// 1. Avoided unnecessary string to array conversions.
// 2. Eliminated the use of 'shift' operation on strings (O(n)).
// 3. Removed the modulo 2 calculation to control which string to choose.
// 4. Avoided the use of 'while' loops.
var mergeAlternately = function(word1, word2) {
    let result = "";
    const maxLength = Math.max(word1.length, word2.length);

    for (let i = 0; i < maxLength; i++) {
        if (i < word1.length) {
            result += word1[i];
        }
        if (i < word2.length) {
            result += word2[i];
        }
    }

    return result;
};

After carefully analyzing my initial solution and referring to Vikas's solution, I realized that I could make several optimizations. In the optimized version, I eliminated the unnecessary conversion of strings to arrays. Instead, I directly accessed characters from word1 and word2 using simple indexing.

I also got rid of the count variable and the while loops, making the code more efficient and easier to follow. The use of a for loop allowed me to iterate through both input strings up to the maximum length, merging the characters alternately.

Conclusion

Optimizing my initial solution code was a rewarding experience, it taught me the importance of thinking critically about the code efficiency and readability. By eliminating unnecessary operations and simplifying the logic, I was able to achieve a more elegant and efficient solution. I encourage you to practice optimization skills, as they are valuable not only in coding interviews but also in real-world programming.

I hope this blog post helps you understand the process of moving from an initial solution to an optimized one, and I look forward to sharing more coding insights in the future. Happy coding! ๐Ÿ˜

ย