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
47
48
49
50
51
52
53
54
55
import java.util.*;
class Solution {
    public int[] solution(String msg) {
        int[] answer = {};
       
        HashMap<String, Integer> hm = new HashMap<>();
        HashMap<String, Boolean> checkSearch = new HashMap<>();
        String[] alphabet = {"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"};
        
        //알파벳 인입 
        for(int i=1; i<=26; i++){
            hm.put(alphabet[i-1], i);
        }
        
        //현재 입력
        String currentInput = "";
        //다음 글자
        String nextWord = "";
        //출력
        int output = 0;
        String searchLetter = ""
        //꼼수, 현재값, 다음 글자 값을 계속 들고있어야 해서 -1 만큼 보아야 하기 때문에 
        msg+=" ";
        String[] msgSplit = msg.split("");
        ArrayList<Integer> al = new ArrayList<>();
        for(int i=0; i<msgSplit.length-1; i++){
            //현재 입력, 다음 글자
            //NOTICE : 사전추가 값이 없으면 현재 입력을 유지하지만, 사전 추가 값이 있으면 현재 입력을 다음 msg 값을 추가한다. 
            
            currentInput +=  msgSplit[i];
            nextWord = msgSplit[i+1];
            
            //사전추가
            searchLetter = currentInput + nextWord; 
            
            //값이 있으면 
            if(checkSearch.containsKey(searchLetter)) continue;
            
            //출력을 위해 사전 추가 된 값을 +1 씩 채워준다. 
            hm.put(searchLetter, hm.size()+1);
            
            //동적할당이 안되서 ArrayList에 추가한다. answer를 return 해 줄려고
            al.add(hm.get(currentInput));
            checkSearch.put(searchLetter, true);
            
            //현재입력을 초기화
            currentInput = "";
            searchLetter = "";
        }
        answer = new int[al.size()];
        for(int i=0; i<al.size(); i++)
            answer[i] = al.get(i);
        return answer;
    }
}
cs

'알고리즘' 카테고리의 다른 글

새로운게임_Java  (0) 2021.08.04
LeetCode : Partition Label  (0) 2021.07.07
Kakao_보석쇼핑  (0) 2021.06.10
BOJ_14864_줄서기  (0) 2021.05.30
BOJ 5719 거의 최단 경로  (0) 2021.05.27