Largest Merge Of Two Strings LeetCode solution in JavaScript
1var largestMerge = function (word1, word2) {2let ans = "";3let w1 = 0,4w2 = 0;56while (w1 < word1.length && w2 < word2.length) {7if (word1[w1] > word2[w2]) ans += word1[w1++];8else if (word2[w2] > word1[w1]) ans += word2[w2++];9else {10const rest1 = word1.slice(w1);11const rest2 = word2.slice(w2);1213if (rest1 > rest2) ans += word1[w1++];14else ans += word2[w2++];15}16}17ans += word1.slice(w1);18ans += word2.slice(w2);19return ans;20};