DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 1768. Merge Strings Alternately

Heres , the solution try to understand the code šŸ™ƒ

/**
 * @param {string} word1
 * @param {string} word2
 * @return {string}
 */
var mergeAlternately = function(word1, word2) {

    const res = [];//final result array

    //looping sufficient for every char in both words 

    for(let i = 0 ;i < word1.length + word2.length ;i++){

        //pushing chars in word1
        if(i<word1.length){
            res.push(word1[i])
        }

        //pushing chars in word1
          if(i<word2.length){
            res.push(word2[i])
        }
    }
    return res.join('')
};
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
rakeshreddy512 profile image
Rakesh Reddy Peddamallu • Edited

Please do follow the channel , will be updating more solutions and patterns