[프로그래머스] 카카오_압축 (자바 풀이)
- 알고리즘 문제 해결(PS)/[프로그래머스]
- 2022. 2. 14.
문제
https://programmers.co.kr/learn/courses/30/lessons/17684
코딩테스트 연습 - [3차] 압축
TOBEORNOTTOBEORTOBEORNOT [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]
programmers.co.kr
풀이
간단한 구현 문제이다. 문제에서 하라는대로만 잘 구현하면 되는 어려울게 없는 문제이다.
우선 사전으로 사용할 HashMap<String, Integer>를 선언해 'A'부터 'Z'까지 저장한다.
이후 한 글자씩 판단하면서 w와 c를 적절히 찾아서 사전에 추가해주면 된다.
자세한건 코드를 참고하자.
코드
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.util.*; class Solution {     HashMap<String, Integer> dict = new HashMap<>();     public int[] solution(String msg) {         int[] answer;         ArrayList<Integer> ans = new ArrayList<>();                 int dictIdx = 1;         for(int i='A'; i<='Z'; i++){             dict.put( String.valueOf((char)i), dictIdx++) ;         }         int idx = 0;         while(idx < msg.length()){             String w = "";             while(idx < msg.length()){                 if(!dict.containsKey(w+msg.charAt(idx))){                     break;                 }else{                     w += msg.charAt(idx);                 }                 idx++;             }             ans.add(dict.get(w));             if(idx < msg.length()){                 dict.put(w+msg.charAt(idx), dictIdx++);             }                     }         answer = new int[ans.size()];         for(int i =0; i<ans.size(); i++){             answer[i] = ans.get(i);         }         return answer;     } } | cs | 
반응형
    
    
    
  '알고리즘 문제 해결(PS) > [프로그래머스]' 카테고리의 다른 글
| [프로그래머스] 카카오_길찾기 게임 (자바 풀이) (0) | 2022.02.22 | 
|---|---|
| [프로그래머스] 카카오_파일명 정렬 (자바 풀이) (1) | 2022.02.15 | 
| [프로그래머스] 카카오_방금그곡 (자바 풀이) (0) | 2022.02.14 | 
| [프로그래머스] 카카오_추석 트래픽 (자바 풀이) (0) | 2022.01.25 | 
| [프로그래머스] 카카오_셔틀버스 (자바 풀이) (0) | 2022.01.25 |