CS/알고리즘_KAKAO BLIND RECRUITMENT

2018 KAKAO BLIND RECRUITMENT : [3차] 압축

Jedy_Kim 2021. 10. 12. 09:10
728x90

https://programmers.co.kr/learn/courses/30/lessons/17684

 

코딩테스트 연습 - [3차] 압축

TOBEORNOTTOBEORTOBEORNOT [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]

programmers.co.kr

 

// 코드

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
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
 
class Solution {
    public int[] solution(String msg) {
        
        ArrayList<String> index = new ArrayList<>(Arrays.asList(new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V"
                ,"W","X","Y","Z"}));
        
        if(msg.length() == 1) {
            int i = index.indexOf(msg);            
            return new int[]{ i+1 };
        }
        
        ArrayList<Integer> res = new ArrayList<>();
        String w = String.valueOf(msg.charAt(0));
        int idx = 1;
                
        while(idx <= msg.length()) {
            
            if(idx == msg.length()) {
                res.add((index.indexOf(w) + 1));
                break;
            }
            
            char c = msg.charAt(idx);
            
            String wc = w + c;
            
            if(index.contains(wc)) {
                w = wc;
                idx++;
                continue;
            }
            
            index.add(wc);
            res.add(index.indexOf(w) + 1);
            
            w = String.valueOf(c);
            idx++;
        }
        
        int[] answer = new int[res.size()];
        for(int i=0; i<answer.length; i++) answer[i] = res.get(i);
        return answer;
    }
}
cs

 

반응형