매일 조금씩

Leet code (Easy): 1768. Merge Strings Alternately - Java 본문

알고리즘/String

Leet code (Easy): 1768. Merge Strings Alternately - Java

mezo 2024. 11. 19. 21:00
728x90
반응형

 

 

class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder merged = new StringBuilder();
        int maxLength = Math.max(word1.length(), word2.length());

        for(int i = 0; i < maxLength; i++){
            if(i < word1.length()){
                merged.append(word1.charAt(i));
            }
            if(i < word2.length()){
                merged.append(word2.charAt(i));
            }
        }

        return merged.toString();
        
    }
}

 

 

 

 

728x90
반응형