기타/코딩테스트

(코테) 프로그래머스_[3차] 압축 *정렬 문자열 변환

불광동 물주먹 2025. 7. 30. 03:37

 

 

 

 

import java.util.*;

class Solution {
    public int[] solution(String msg) {
        int[] answer = {};
            
        
        Map<String,Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        
        int idx=1;
        for(char c='A';c<='Z';c++){
            map.put(String.valueOf(c),idx++);            
        }
        int j=0;
        
        //msg 길이로 순회
        while(j<msg.length()){
            String w= "";            
            int nIndex=j+1;
            
            // index 부터 최대 길이까지 순회
            while( nIndex<=msg.length() && map.containsKey(msg.substring(j,nIndex))){                
                w=msg.substring(j,nIndex);
                nIndex++;                
            }
            
            list.add(map.get(w));            
            
            if(nIndex <= msg.length()){
                map.put(msg.substring(j,nIndex),idx++);                
            }
            j += w.length();
        }             
        
        
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

 

1. 알파벳  for문 돌기 char로 ++ 해주면 순회 가능

 

2. stream  List<Integer> - int[]  변환 

mapToInt에서 (Integer::intValue)