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)
'기타 > 코딩테스트' 카테고리의 다른 글
| (코테) 프로그래머스_기둥과 보 설치 *시물레이션+구현 (3) | 2025.08.02 |
|---|---|
| (코테) 프로그래머스_셔틀버스 *시물레이션 정렬 (3) | 2025.07.30 |
| (코테) 프로그래머스_여행경로 *dfs 백트레킹 (3) | 2025.07.23 |
| (코테) 프로그래머스_섬 연결하기 *유니온 파인드 (3) | 2025.07.17 |
| (코테) 프로그래머스_입국심사 *이진탐색 (0) | 2025.07.16 |