
문제
[프로그래머스] 의상

풀이
- 의상 종류별 수를 해시맵에 저장한다.
- 이미 키 값이 존재하면, 기존 값에 1을 더함
- 존재하지 않는 경우, 2 저장
(해당 종류의 의상을 입지 않는 경우를 고려)
- 의상 종류별 조합을 구하기 위해 저장된 값을 모두 곱해준다.
- 아무 것도 입지 않는 한 가지의 경우를 빼준다.
정답 코드
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
int answer = 1;
Map<String, Integer> hm = new HashMap<>();
for(int i = 0; i < clothes.length; i++) {
String type = clothes[i][1];
if(hm.containsKey(type)) {
hm.put(type, hm.get(type)+1);
} else {
hm.put(type, 2);
}
}
for(int value : hm.values()) {
answer *= value;
}
return (answer - 1);
}
}