[LeetCode][Java] 1768. Merge Strings Alternately

2026. 4. 16. 02:45·알고리즘 & 자료구조/문제 풀이

https://leetcode.com/problems/merge-strings-alternately/description/?envType=study-plan-v2&envId=leetcode-75

 

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

 

word1부터 번갈아가면서 하나의 문자열이 끝날 때까지 append 
그 이후 글자가 남은 단어의 글자를 하나씩 append

import java.util.*;

class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder sb = new StringBuilder();
        int length1 = word1.length();
        int length2 = word2.length();
        int word1Idx = 0;
        int word2Idx = 0;
        while(word1Idx < length1 && word2Idx < length2) {
            sb.append(word1.charAt(word1Idx++));
            sb.append(word2.charAt(word2Idx++));
        }
        while(word1Idx < length1) {
            sb.append(word1.charAt(word1Idx++));
        }
        while(word2Idx < length2) {
            sb.append(word2.charAt(word2Idx++));
        }
        return sb.toString();
    }
}

 

alternately : 번갈아
alternating : 교차로

저작자표시 비영리 변경금지 (새창열림)

'알고리즘 & 자료구조 > 문제 풀이' 카테고리의 다른 글

[프로그래머스 알고리즘 고득점 Kit][이분탐색][Java] 징검다리  (0) 2026.03.31
[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 순위  (0) 2026.03.29
[프로그래머스 알고리즘 고득점 Kit][힙(Heap)][Java] 더 맵게  (0) 2026.03.29
[프로그래머스 알고리즘 고득점 Kit][깊이/너비 우선 탐색(DFS/BFS)][Java] 네트워크  (0) 2026.03.08
[프로그래머스 알고리즘 고득점 Kit][그래프][Java] 가장 먼 노드  (0) 2026.03.07
'알고리즘 & 자료구조/문제 풀이' 카테고리의 다른 글
  • [프로그래머스 알고리즘 고득점 Kit][이분탐색][Java] 징검다리
  • [프로그래머스 알고리즘 고득점 Kit][그래프][Java] 순위
  • [프로그래머스 알고리즘 고득점 Kit][힙(Heap)][Java] 더 맵게
  • [프로그래머스 알고리즘 고득점 Kit][깊이/너비 우선 탐색(DFS/BFS)][Java] 네트워크
수수다
수수다
우하하
  • 수수다
    그냥살자
    수수다
  • 전체
    오늘
    어제
    • 분류 전체보기 (35) N
      • 프로젝트 (1)
      • 알고리즘 & 자료구조 (18) N
        • 내용 정리 (2)
        • 문제 풀이 (16) N
      • 데이터베이스 (12) N
        • 내용 정리 (1) N
        • 문제 풀이 (11) N
      • CS (2)
      • 기타 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 네이버 블로그
  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    그래프
    프로그래머스
    leetcode
    이분탐색
    바이브코딩
    싸피
    bfs
    Java
    코딩테스트
    DisjointSet
    평균회귀
    코테
    매개변수탐색
    바킹독
    HTTP 메서드
    분리집합
    유니온파인드
    코팅테스트
    mysql
    삼성청년SW·AI아카데미
    SSAFY
    해시
    완전탐색
    프로그래머스 알고리즘 고득점 kit
    디스조인트셋
    깊이/너비 우선 탐색(DFS/BFS)
    귀멸의칼날
    SQL
    유클리드호제법
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
수수다
[LeetCode][Java] 1768. Merge Strings Alternately
상단으로

티스토리툴바