๐ก ์ง์ ํผ ์ฝ๋
class Solution {
public String solution(String code) {
String answer = "";
String ret = "" ;
int mode = 0;
int idx = 0;
for (idx = 0; idx < code.length(); idx++){
if (mode == 0){
if (code.charAt(idx) != '1'){
if(idx % 2 == 0) {
ret += code.charAt(idx) ;
}
}
else if (code.charAt(idx) == '1') {
mode = 1;}
}
else if (mode == 1) {
if (code.charAt(idx) != '1'){
if(idx % 2 != 0) {
ret += code.charAt(idx);
}
}
else if (code.charAt(idx) == '1') {
mode = 0;
}
}
}
if (ret == ""){
ret = "EMPTY";
}
return ret;
}
}
๐ก ์ข์ ์ฝ๋
class Solution {
public String solution(String code) {
StringBuilder answer = new StringBuilder();
int mode = 0;
for (int i = 0; i < code.length(); i++) {
char current = code.charAt(i);
if (current == '1') {
mode = mode == 0 ? 1 : 0;
continue;
}
if (i % 2 == mode) {
answer.append(current);
}
}
return answer.length() == 0 ? "EMPTY" : answer.toString();
}
}