

나의 풀이
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < clothes.length; i++) { // 1
map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
}
Iterator<Integer> it = map.values().iterator(); // 2
int answer = 1;
while(it.hasNext()) // 3
answer *= it.next().intValue() + 1;
return answer - 1; // 4
}
}
과정
- clothes에 담긴 요소들을 키 밸류로 map에 삽입
- map의 모든 값을 컬렉션 형태로 iterator에 삽입
- iterator를 순회해 다음 요소가 없을 때까지 반복, +1은 해당 옷을 입지 않은 경우를 포함하기 위해 추가
- 조합의 수를 곱한 결과에서 1을 빼서 반환(모든 옷을 입지 않은 경우)
다른 사람 풀이
import java.util.*;
import static java.util.stream.Collectors.*;
class Solution {
public int solution(String[][] clothes) {
return Arrays.stream(clothes)
.collect(groupingBy(p -> p[1], mapping(p -> p[0], counting())))
.values()
.stream()
.collect(reducing(1L, (x, y) -> x * (y + 1))).intValue() - 1;
}
}