[Programmers_CT] 30. 문자열 섞기

문정준's avatar
Mar 21, 2025
[Programmers_CT] 30. 문자열 섞기
 
💡
프로그래머스 코딩 테스트 28. 문자열 섞기

1. 문제 설명

notion image
 

제한 사항

notion image

2. 입출력 예시

notion image

3. 코드 작성

class Solution { public String solution(String str1, String str2) { String answer = ""; for(int i=0;i<str1.length();i++) { answer += str1.charAt(i); answer += str2.charAt(i); } return answer; } }
✏️

두 문자열을 번갈아가며 붙이기

  • 두 문자열의 길이가 같으므로 1개의 반복문만 사용하면 됨
    • str1의 i번째 글자를 먼저 더하고, str2의 i번째 글자를 그 뒤에 더함
    • for(int i=0;i<str1.length();i++) { answer += str1.charAt(i); answer += str2.charAt(i); }

4. 테스트 실행

notion image

5. 제출 후 채점

notion image
notion image
Share article

sxias