1768. Merge Strings Alternately
#array #string
Given two strings merge the strings by adding letters in alternating order, starting with word1
. If a string is longer than the other, append the additional letters onto the end of the merged string.
Intuition
Iterate and keep adding characters from the string alternatively
Approach
Iterate and add character alternatively till we reach the end of the string word1 or word2
Add the remaining character as it is
Complexity
Time complexity : O(m+n)
where the m,n are the length of the string word1 & word2
Space complexity : O(m+n)
we store the merged string
Code
Last updated