
나의 풀이
class Solution {
public String solution(String s) {
String answer = "";
String[] str = s.split("");
int idx = 0;
for(int i=0; i<str.length; i++) {
if(str[i].equals(" ")) {
idx = 0;
} else if(idx % 2 == 0) {
str[i] = str[i].toUpperCase();
idx++;
} else if(idx % 2 != 0) {
str[i] = str[i].toLowerCase();
idx++;
}
answer += str[i];
}
return answer;
}
}
다른 사람의 풀이
class Solution {
public String solution(String s) {
String answer = "";
int cnt = 0;
String[] array = s.split("");
for(String ss : array) {
cnt = ss.contains(" ") ? 0 : cnt + 1;
answer += cnt%2 == 0 ? ss.toLowerCase() : ss.toUpperCase();
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 크기가 작은 부분 문자열(Java) * (0) | 2024.11.27 |
|---|---|
| [프로그래머스] 삼총사(Java) (0) | 2024.11.26 |
| [프로그래머스] 3진법 뒤집기(Java) * (0) | 2024.11.24 |
| [프로그래머스] 최대공약수와 최소공배수(Java) (0) | 2024.11.23 |
| [프로그래머스] 직사각형 별찍기(Java) (0) | 2024.11.22 |