[프로그래머스] [LV2] [3차] 압축
업데이트:
📚 [3차] 압축
링크📎 : https://programmers.co.kr/learn/courses/30/lessons/17684
난이도 ⭐️⭐️
📖 문제
📝 내 풀이
import java.util.*;
class Solution {
public int[] solution(String msg) {
int[] answer = {};
Queue<Integer> q = new LinkedList<Integer>();
ArrayList<String> dictionary = new ArrayList<String>();
dictionary.add("0");
for(char c = 'A'; c <= 'Z'; c++)
dictionary.add(c+"");
for(int i = 0; i < msg.length(); i++){
for(int j = i+1; j <= msg.length(); j++){
if(dictionary.indexOf(msg.substring(i,j)) == -1){
dictionary.add(msg.substring(i,j));
q.offer(dictionary.indexOf(msg.substring(i,j-1)));
i = j-2;
break;
}
else if(j == msg.length()){
q.offer(dictionary.indexOf(msg.substring(i,j)));
i = j-1;
break;
}
}
}
answer = new int[q.size()];
int idx = 0;
while(!q.isEmpty())
answer[idx++] = q.poll();
return answer;
}
}
👊🏻 내 전략
- 단어를 관리할 dictionary는 ArrayList로 선언하였다.
- Queue를 선언하여 인덱스를 관리하도록 하였다.
- substring과 indexOf를 적절히 활용하여 단어를 도출하고, 검색하는데 사용하였다.
끝-!
댓글남기기