Merge Strings Alternately

Run Settings
LanguageJavaScript
Language Version
Run Command
//1768. Merge Strings Alternately //step1: input: two string, word1: "abc", word2: "pqrcst" //step2: output: one merged string in alternating order. merged: "apbqcrcst" //step3: pseudoCode: We use Two Pointer //while p1<word1.length or p2<word2.length //if(p1<word1.length) //Write char at p1 //p1++ //if(p2<word2.length) //write char at p2 //p2++ function MergeStrings(input1, input2){ let p1 = 0; let p2 = 0; const result = []; while(p1 < input1.length || p2 < input2.length){ if(p1 < input1.length ){ result.push(input1[p1]); p1++; } if(p2 < input2.length ){ result.push(input2[p2]); p2++; } } return result.join(''); } const myMergeStrings = MergeStrings("abc", "pqrcst"); console.log(myMergeStrings); //TimeComplexity: O(n+m), SpaceComplexity: O(n+m) //EdgeCases: 1- both are empty or null, 2- one is empty 3- very long string 4- identical strings 5- nonstrings
Editor Settings
Theme
Key bindings
Full width
Lines