[프로그래머스] 해시 - 위장(자바)

Rena·2022년 3월 30일
0

알고리즘 문제풀이

목록 보기
20/45
import java.util.HashMap;
import java.util.Iterator;

class Solution {
    public int solution(String[][] clothes) {
        HashMap<String,Integer> map = new HashMap<>();
        for(String[] clothe : clothes) {
            String type = clothe[1];
            map.put(type,map.getOrDefault(type,0)+1);
        }

        Iterator<Integer> it = map.values().iterator();

        int answer = 1;
        while(it.hasNext())
            answer *= it.next().intValue() +1; //옷을 안입을 경우
        return answer - 1; //모든 옷을 안입을 경우는 제외
    }

    public static void main(String[] args) {
        String[][] clothes = {{"yellowhat","headwear"},{"bluesunglasses","eyewear"}};
        Solution solution = new Solution();
        System.out.println(solution.solution(clothes));
    }
}
profile
일을 사랑하고 싶은 개발자

0개의 댓글