15% off all plans & roadmaps Use code
See plansProblem editorial
Implement mergeAlternately to combine the characters of word1 and word2 into one string. Begin with the first character of word1, then the first character of word2, and continue by taking the next character from each string in turn whenever that string still has an unused character.
Merge Strings Alternately is a Two Pointers problem, in the In-place Array Modification subpattern. The pattern covers fast-slow, converging, expanding from center, and in-place modification techniques
The reference solution runs in O(word1.length() + word2.length()) time and O(word1.length() + word2.length()) for the returned string; O(1) auxiliary space excluding the output space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Merge Strings Alternately is a Two Pointers problem, in the In-place Array Modification subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.