-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
42 lines (34 loc) · 1.16 KB
/
Result.java
File metadata and controls
42 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package hackrank.algorithm.string.gems;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @see <a href="https://www.hackerrank.com/challenges/gem-stones">Gemstones</a>
*/
public class Result {
/**
* @param rocks List of strings. Each string character represents mineral in the rock
* @return Number of gemstones found in {@code rocks}
*/
public static int gemstones(List<String> rocks) {
Map<Character, Set<Integer>> materialCounts = new HashMap<>();
for (int rockId = 0; rockId < rocks.size(); rockId++) {
String rock = rocks.get(rockId);
for (char material : rock.toCharArray()) {
if (!materialCounts.containsKey(material)) {
materialCounts.put(material, new HashSet<>());
}
materialCounts.get(material).add(rockId);
}
}
int countGems = 0;
for (Set<Integer> rockIds : materialCounts.values()) {
if (rockIds.size() == rocks.size()) {
countGems++;
}
}
return countGems;
}
}