[leetcode, JS] 1768. Merge Strings Alternately

mxxn·2023년 11월 8일
0

leetcode

목록 보기
115/198

문제

문제 링크 : Merge Strings Alternately

풀이

/**
 * @param {string} word1
 * @param {string} word2
 * @return {string}
 */
var mergeAlternately = function(word1, word2) {
    let result = ''
    for(let i=0; i<Math.max(word1.length, word2.length); i++) {
        if(i < word1.length) {
            result += word1[i]
        }
        if(i < word2.length) {
            result += word2[i]
        }
    }

    return result;
};
  1. word1과 word2의 length의 max만큼 for문을 진행하고
  2. 값이 존재할때만 result에 더해가는 방식
  • Runtime 48ms, Memory 42.60MB
profile
내일도 글쓰기

0개의 댓글