Lined Notebook

[1234] [Java] 비밀번호

by HeshAlgo

<비밀번호>

문제 설명

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14_DEKAJcCFAYD&categoryId=AV14_DEKAJcCFAYD&categoryType=CODE

 

내 생각

붙어있는 문자열이 같은 경우를 찾기로 했습니다. 붙어있는 문자열이 같을 경우, 해당 문자열들을 삭제하고 2개씩 삭제가 되니 길이도 2만큼 줄였습니다. 이후, 찾을 인덱스 값을 0으로 초기화 후 다시 붙어있는 문자열이 있는지 확인했습니다. 더 이상 붙어있는 문자열이 없을 경우 최종 문자열이 비밀번호가 되므로 출력하면 끝이 납니다.

 

푼 시간

12분 51초

 

작성 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 비밀번호
import java.io.FileInputStream;
import java.util.*;
 
public class E1234 {
 
    public static void main(String args[]) throws Exception {
        String path = "input.txt";
        System.setIn(new FileInputStream(path));
        Scanner sc = new Scanner(System.in);
        
        for(int test_case = 1; test_case <= 10; test_case++) {
            int len = sc.nextInt();
            String password = sc.next();
            StringBuffer sb = new StringBuffer(password);
            int index = 0;
            
            while (index < len-1) {
                if (sb.charAt(index) == sb.charAt(index+1)) {
                    sb.deleteCharAt(index);
                    sb.deleteCharAt(index);
                    index = 0;
                    len -= 2;
                    continue;
                } 
                index++;
            }
            System.out.println("#" + test_case + " " + sb);
        }
        
    }
    
}
 
 

 

실행 결과

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기