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 rocks) { int countInRocks = rocks.size(); Map potential = new HashMap<>(); for(String rock: rocks) { Map 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 rocks = IntStream.range(0, rocksCount).mapToObj(i -> { return scanner.nextLine(); }).collect(toList()); int result = gemstones(rocks); System.out.println(result); } }