문제
https://school.programmers.co.kr/learn/courses/30/lessons/42578
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
HashMap의 getOrDefault를 이용하여 해당하는 옷종류의 값을 1씩 추가해준다.
옷 조합의 개수는 hash 결과값을 곱해서 구하면 된다.
다만 옷이 포함될수 있고 안포함 될수도 있다. 포함되는것은 이미 구해졌으므로 포함이 안될경우도 더해줘야 한다. 포함이 안될경우는 1이다. 따라서 hash값에 +1을 하여 곱해주면 된다.
코드
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
Map<String, Integer> map = new HashMap<>();
for(String[] clothe : clothes){
String cloth = clothe[1];
map.put(cloth, map.getOrDefault(cloth, 0)+1);
}
int answer = 1;
Iterator<Integer> it = map.values().iterator();
while(it.hasNext()) {
answer *= (it.next().intValue()+1);
}
return answer-1;
}
}