위장

eunseo·2021년 7월 7일

Programmers

목록 보기
5/14

import java.util.*;


public class 위장 {
    public int solution(String[][] clothes) {
        int answer = 1;
            
        Map<String,Integer> map = new HashMap<>();
        
        for(int i=0; i< clothes.length; i++){
            map.put(clothes[i][1], map.getOrDefault(clothes[i][1],0)+1);
        }
        
        for(int value : map.values()){
            answer *= (value+1);
        }

        return answer-1;
    }
}

getOrDefault 메서드는 key가 이미 map에 있을 경우 그 값을 반환하고 없으면 기본값인 0을 넣는다.

profile
backend developer

0개의 댓글