본문 바로가기
Problem Solving

[프로그래머스] L2 방금그곡 / 2018 카카오 블라인드 채용 (Java)

by JYHAN 2020. 12. 19.

카카오

[프로그래머스] L2 압축 / 2018 카카오 블라인드 채용

[풀이]

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
import java.util.*;
class Solution {
    public int[] solution(String msg) {
        Map<String, Integer> dict = new HashMap<String, Integer>();
        for(int i=65; i<=90; i++// 1. 길이가 1인 사전
            dict.put(Character.toString((char)i), i-64);
        
        int idx = 27;
        List<Integer> list = new ArrayList<Integer>();
        boolean[] chk = new boolean[msg.length()];
        for(int i=0; i<msg.length(); i++){
            if(chk[i]) continue;
            int add = 0;
            int end = i+1;
            String temp = "";
            while(true){
                temp = msg.substring(i,end);
                if(!dict.containsKey(temp)){
                    dict.put(temp, idx++);
                    // i가 0 end 2일경우 i 변화x
                    break;
                }
                chk[end-1= true;
                add = dict.get(temp);
                if(++end > msg.length())
                    break;
            }
            list.add(add);
        }
        int[] answer = new int[list.size()];
        for(int i=0; i<list.size(); i++)
            answer[i] = list.get(i);
        return answer;
    }
}
cs

댓글