A~Z를 사전에 등록한다.
String word = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0; i<word.length();i++){ dictionary.add(word.substring(i,i+1)); }
현재 문자 : String w = msg.substring(i,i+1);
idx는 현재 인덱스 다음 번호이다.
이 인덱스가 msg의 길이보다 큰 경우는 w가 마지막 글자라는 뜻이다.
그렇기에 while문을 돌지 않고 바로 arr에 값을 넣어준다.
현재 문자 w와, 다음 문자 c를 합쳐 임시 문자 temp를 만들고
그 단어가 서전에 없다면 if(dictionary.indexOf(temp)==-1)
temp를 단어에 넣고 break,그 단어가 사전에 있다면 현재 문자는 temp가 되고, 다음 for문에서 idx를 다시 돌지 않기 위해서
i++, idx++를 해준다.
import java.util.*;
class Solution {
    public int[] solution(String msg) {
        List<String>dictionary = new ArrayList();
        String word = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for(int i=0; i<word.length();i++){
            dictionary.add(word.substring(i,i+1));
        }
        //뽑는값은 indexOf("문자")+1
        List<Integer>arr = new ArrayList();
        
        for(int i=0; i<msg.length(); i++){
            String w = msg.substring(i,i+1);
            int idx = i+1;
            while(idx<msg.length()){
                String c = msg.substring(idx,idx+1);
                String temp = w+c;
                if(dictionary.indexOf(temp)==-1){
                    dictionary.add(temp);
                    break;
                } else {
                    w = temp;
                    i++;
                    idx++;
                }
            }
            arr.add(dictionary.indexOf(w)+1);           
        }    
        
        int[] answer = new int[arr.size()];
        for(int i=0; i<answer.length; i++){
            answer[i] = arr.get(i);
        }
        
        return answer;
    }
}