[프로그래머스]해시-위장

snusun·2021년 1월 27일
0

조합을 써서 풀면 굉장히 간단하다.

(1) 옷 종류마다 개수를 센다
(2) +1 하여 다 곱하면 옷을 입는 가짓수를 구할 수 있다.
(3) -1 하여 아무것도 안입는 경우를 제한다.

import java.util.*;

class Solution {
    public int solution(String[][] clothes) {
        int answer = 1;
        HashMap<String,Integer> type = new HashMap<String,Integer>();
        for(int i=0; i<clothes.length; i++){
            if(type.containsKey(clothes[i][1])){
                type.replace(clothes[i][1], type.get(clothes[i][1])+1);
            } else {
                type.put(clothes[i][1], 1);
            }
        }

        for(String s: type.keySet()){
            answer *= type.get(s)+1;
        }
        return answer-1;
    }
}
profile
대학생 근데 이제 컴공을 곁들인

0개의 댓글

관련 채용 정보