Leetcode Practice
LC 1768. Merge Strings Alternately
- Last updated
- Reading time
- 1 min read
The problem
Time
Solution -I came back to do an easy one after doing many harder stack-based problems, so I felt refreshed here 😅. Check the max length to iterate to, then add letters to the string if they exist. Once a word runs out, the other will continue to push characters.
function mergeAlternately(word1: string, word2: string): string {
let answer = ''
for (let i = 0; i < Math.max(word1.length, word2.length); i++) {
if (word1[i]) {
answer += word1[i]
}
if (word2[i]) {
answer += word2[i]
}
}
return answer
}