Skip to content

Instantly share code, notes, and snippets.

@Artm1n3r
Created February 21, 2020 21:29
Show Gist options
  • Save Artm1n3r/2aafd882a978411f98704971b4552be8 to your computer and use it in GitHub Desktop.
Save Artm1n3r/2aafd882a978411f98704971b4552be8 to your computer and use it in GitHub Desktop.
Algorithm - Gemstones
package xyz.smile_h.bit.algorithm.hr.gem_stones;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toList;
public class Solution {
public static int gemstones(List<String> rocks) {
int countInRocks = rocks.size();
Map<String, Long> potential = new HashMap<>();
for(String rock: rocks) {
Map<String, Long> counter = new HashMap<>();
Arrays.stream(rock.split("")).forEach(w -> {
counter.put(w, counter.containsKey(w) ? counter.get(w) + 1 : 1);
});
counter.forEach((k, v) -> {
if(v >= 1) {
potential.put(k, potential.containsKey(k) ? potential.get(k) + 1 : 1);
}
});
}
int result = 0;
for(Long v : potential.values()){
if(v == countInRocks) {
result++;
}
}
return result;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("src/main/java/xyz/smile_h/bit/algorithm/hr/t3/case.txt"));
int rocksCount = Integer.parseInt(scanner.nextLine().trim());
List<String> rocks = IntStream.range(0, rocksCount).mapToObj(i -> {
return scanner.nextLine();
}).collect(toList());
int result = gemstones(rocks);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment