알고리즘/programmers
[Programmer] [3차] 압축
akjfal
2021. 6. 23. 00:21
// 사전 초기화 w 제거 및 w 의 index출력
// 남은글자 w+c를 사전에 등록
import java.util.LinkedList;
import java.util.HashMap;
class Solution {
String[] diction;
HashMap<String, Integer> map = new HashMap<>();
public int[] solution(String msg) {
int[] answer = {};
LinkedList<Integer> list = new LinkedList<>();
char startChar = 'A';
int recentIdx = 0;
for(int i = 0; i < 27; i++){
map.put(Character.toString(startChar+i), ++recentIdx);
}
int start = 0;
int end = 1;
int length = msg.length();
while(start < length){
int lastIdx = 0;
while(start < length){
String test = msg.substring(start, end);
if(map.containsKey(test)){
lastIdx = map.get(test);
end++;
if(end == length+1){
start = length;
list.add(lastIdx);
break;
}
}else{
map.put(test, recentIdx++);
list.add(lastIdx);
start = end-1;
break;
}
}
}
answer = new int[list.size()];
int i = 0;
for(int num : list){
answer[i] = num;
i++;
}
return answer;
}
}
딱히 어려울것없이 흐름대로 풀면된다.