[Java] 21. 코드 처리하기

김주희's avatar
Jun 10, 2025
[Java] 21. 코드 처리하기

1. 문제 설명, 제한 사항, 입출력 예시

notion image
notion image
notion image
 

2. 아이디어

💡
String과 char의 차이
 

3. 풀이 코드

class Solution { public String solution(String code) { int mode = 0; String ret = ""; for(int i = 0; i < code.length(); i++){ String c = code.charAt(i)+""; if(mode == 0) { if(c.equals("1")){ mode = 1; }else{ ret += i % 2 == 0 ? c : ""; } } else { if(c.equals("1")){ mode = 0; }else{ ret += i % 2 != 0 ? c : ""; } } } if (ret.equals("")) { return "EMPTY"; }else{ return ret; } } }
GPT
class Solution { public String solution(String code) { StringBuilder ret = new StringBuilder(); int mode = 0; for (int i = 0; i < code.length(); i++) { char c = code.charAt(i); if (c == '1') { mode = (mode == 0) ? 1 : 0; if ((mode == 0 && i % 2 == 0) || (mode == 1 && i % 2 != 0)) { ret.append(c); } } } return ret.length() == 0 ? "EMPTY" : ret.toString(); } }
Share article

jay0628