프라이D
프라이Develog(❁´◡`❁)
프라이D
전체 방문자
오늘
어제
  • ALL (378)
    • TDD, Cleancode with JavaScr.. (5)
    • 프로젝트 (32)
      • work (3)
      • 직접 만드는 기술 블로그 (2)
      • 데일리 옥션 (19)
      • 모락모락 (8)
    • Computer Science (1)
    • Algorithm & 자료구조 (94)
      • 알고리즘 w.JavaScript (53)
      • 자료구조 (5)
      • (인프런) 자바스크립트 알고리즘 문제풀이 (34)
    • JavaScript (45)
      • JavaScript (41)
      • 모던 자바스크립트 Deep Dive (4)
    • WEB (13)
    • 회고 (12)
    • TIL (109)
    • WIL (7)
    • Stacks (20)
      • React.js (6)
      • Next.js (1)
      • Redux (3)
      • Node.js (2)
      • GIT (2)
      • SAP (1)
    • 15일 메이킹 프로젝트 (15)
    • 이전 기록 (14)
    • ETC. (5)
    • ---------------2021 (6)
      • 내일배움단-웹개발 5주 (2)
      • 정보처리기사 (4)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 스파르타코딩클럽
  • JavaScript
  • nomadcoder
  • MySQL
  • 자바스크립트알고리즘
  • Til
  • nomadcoders
  • 모던자바스크립트딥다이브
  • 비트마스크
  • 내일배움단
  • 코드스테이츠
  • 2023 인프콘 후기
  • 자바스크립트비트마스크
  • vanilaJS
  • 내일배움카드
  • 국비지원
  • 코딩프로젝트
  • 알고리즘
  • 자바스크립트
  • 투포인터알고리즘

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
프라이D

프라이Develog(❁´◡`❁)

Algorithm & 자료구조/알고리즘 w.JavaScript

[Leetcode] 1768.Merge Strings Alternately

2023. 12. 18. 14:35

 

 

Merge Strings Alternately - LeetCode

Can you solve this real interview question? Merge Strings Alternately - You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional le

leetcode.com

You are given two strings word1 and word2. 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.
Return the merged string.

 

나의 풀이

const mergeAlternately = function (word1, word2) {
    const totalLength = Math.max(word1.length, word2.length)
    const result = [];

    for(let i = 0; i < totalLength; i++) {
        result.push(word1[i])
        result.push(word2[i])
    }

    return result.join('')
};

 

  • join 메서드를 사용하면 배열 값이 null 이거나 undefined 인 경우 빈 문자열로 치환됨
  • 즉 없어짐

추가된 풀이

var mergeAlternately = function(word1, word2) {
    let result = '';

    for(let i = 0; i < Math.max(word1.length, word2.length); i++) {
        result += word1[i] ?? '';
        result += word2[i] ?? '';
    }

    return result;
};
  • nullish 연산자를 사용해서 빈 문자열을 더해버림
저작자표시 (새창열림)

'Algorithm & 자료구조 > 알고리즘 w.JavaScript' 카테고리의 다른 글

[Leetcode] 1431. Kids With the Greatest Number of Candies  (0) 2023.12.31
[Leetcode] 1071. Greatest Common Divisor of Strings  (0) 2023.12.28
[알고리즘 JS] Sliding window - 중복이 없는 가장 긴 문자열의 길이 찾기  (0) 2023.03.24
[알고리즘JS] 피로도 (프로그래머스 lv.2)  (0) 2023.02.04
[알고리즘JS] 연속 부분 수열 합의 개수 (프로그래머스 lv.2)  (0) 2023.02.04
    'Algorithm & 자료구조/알고리즘 w.JavaScript' 카테고리의 다른 글
    • [Leetcode] 1431. Kids With the Greatest Number of Candies
    • [Leetcode] 1071. Greatest Common Divisor of Strings
    • [알고리즘 JS] Sliding window - 중복이 없는 가장 긴 문자열의 길이 찾기
    • [알고리즘JS] 피로도 (프로그래머스 lv.2)
    프라이D
    프라이D
    틀린내용 정정 및 개선사항은 언제든지 댓글 달아주세요 :D

    티스토리툴바