내 풀이
class Solution {
public String solution(String str1, String str2) {
String answer = "";
char[] chaArr1 = str1.toCharArray();
char[] chaArr2 = str2.toCharArray();
for(int i = 0; i<chaArr1.length; i++){
answer = answer + chaArr1[i]+chaArr2[i];
}
System.out.println("answer = "+answer);
return answer;
}
}
다른 사람 풀이
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;
}
}
풀이
charAt 활용
다른 사람 풀이
class Solution {
public String solution(String str1, String str2) {
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i=0; i<chars1.length; i++) {
sb.append(chars1[i]).append(chars2[i]);
}
return sb.toString();
}
}
풀이
mutable 객체인 StringBuilder 활용하여 메모리 사용량을 줄임