@@ -0,0 +1,2359 @@
+package algorithms.strings;%0D%0A%0D%0Aimport java.util.*;%0D%0A%0D%0A/*%0D%0A * Problem Statement: %0D%0A * John has discovered various rocks. Each rock is composed of various elements,%0D%0A * and each element is represented by a lower-case Latin letter from 'a' to 'z'.%0D%0A * An element can be present multiple times in a rock. An element is called a%0D%0A * gem-element if it occurs at least once in each of the rocks.%0D%0A * %0D%0A * Given the list of N rocks with their compositions, display the number of%0D%0A * gem-elements that exist in those rocks.%0D%0A * %0D%0A * Input Format: %0D%0A * The first line consists of an integer, N, the number of rocks.%0D%0A * Each of the next N lines contains a rock's composition. Each composition%0D%0A * consists of lower-case letters of English alphabet.%0D%0A * %0D%0A * Constraints:%0D%0A * 1 %3C= N %3C= 100%0D%0A * Each composition consists of only lower-case Latin letters ('a'-'z').%0D%0A * 1 %3C= length of each composition %3C= 100%0D%0A * %0D%0A * Output Format: %0D%0A * Print the number of gem-elements that are common in these rocks. If there are%0D%0A * none, print 0.%0D%0A * %0D%0A * Sample Input: %0D%0A * 3%0D%0A * abcdde%0D%0A * baccd%0D%0A * eeabg%0D%0A * %0D%0A * Sample Output: %0D%0A * 2%0D%0A */%0D%0Apublic class Gemstones %7B%0D%0A%0D%0A    public static void main(String%5B%5D args) %7B%0D%0A        Scanner sc = new Scanner(System.in);%0D%0A        %0D%0A        HashMap%3CCharacter, Integer%3E elements = new HashMap%3C%3E();%0D%0A        HashSet%3CCharacter%3E eachRockComposition = new HashSet%3C%3E();%0D%0A        %0D%0A        int numRocks = sc.nextInt();%0D%0A        %0D%0A        for (int i = 0; i %3C numRocks; i++) %7B%0D%0A            String comp = sc.next();%0D%0A            for (int j = 0; j %3C comp.length(); j++) %7B%0D%0A                eachRockComposition.add(comp.charAt(j));%0D%0A            %7D%0D%0A            for (Character c : eachRockComposition) %7B%0D%0A                if (elements.containsKey(c)) %7B%0D%0A                    int num = elements.get(c);%0D%0A                    num++;%0D%0A                    elements.replace(c, num);%0D%0A                %7D else %7B%0D%0A                    elements.put(c, 1);%0D%0A                %7D%0D%0A            %7D%0D%0A            eachRockComposition.clear();%0D%0A        %7D%0D%0A        sc.close();%0D%0A        %0D%0A        int commonElements = 0;%0D%0A        %0D%0A        Collection%3CInteger%3E numCommon = elements.values();%0D%0A        %0D%0A        for (int i : numCommon) %7B%0D%0A            if (i %3E= numRocks) %7B%0D%0A                commonElements++;%0D%0A            %7D%0D%0A        %7D%0D%0A        System.out.println(commonElements);%0D%0A    %7D%0D%0A%7D

Add Gemstones