Gemstones – HackerRank Solution

In this post, we will solve Gemstones HackerRank Solution. This problem (Gemstones) is a part of HackerRank Problem Solving series.

Task

There is a collection of rocks where each rock has various minerals embeded in it. Each type of mineral is designated by a lowercase letter in the range ascii[az]. There may be multiple occurrences of a mineral in a rock. A mineral is called a gemstone if it occurs at least once in each of the rocks in the collection.

Given a list of minerals embedded in each of the rocks, display the number of types of gemstones in the collection.

Example

arr = [‘abc’, ‘abc’, ‘bc’]

The minerals b and c appear in each rock, so there are 2 gemstones.

Function Description

Complete the gemstones function in the editor below.

gemstones has the following parameter(s):

  • string arr[n]: an array of strings

Returns

  • int: the number of gemstones found

Input Format

The first line consists of an integer n, the size of arr.
Each of the next n lines contains a string arr[i] where each letter represents an occurence of a mineral in the current rock.

Constraints

  • 1 <= n <= 100
  • 1 <= | arr[i] | <= 100
  • Each composition arr[i] consists of only lower-case Latin letters (‘a’-‘z’).

Sample Input

STDIN       Function
-----       --------
3           arr[] size n = 3
abcdde      arr = ['abcdde', 'baccd', 'eeabg']
baccd
eeabg

Sample Output

2

Explanation

Only a and b occur in every rock.

Solution – Gemstones – HackerRank Solution

C++

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int T;
    int a[26] = {0};
    bool flag[26] = {false};
    int nCount = 0;
     
    cin >> T;
    cin.ignore();
    
    int curTest = 1;
    while(curTest <= T)
    {
    string in;
    getline(cin,in);

    for(int i = 0; i < in.length();i++)
    {
        int ch = ((int)in[i]) - 97;
        if( ch >= 0 && ch < 26 && flag[ch] == false)    
         {          a[ch]++;
                    flag[ch] = true;
         }
        
    }
        for(int i = 0; i < 26;i++)
            flag[i] = false;
   
    
        curTest++;
    }
    
    for(int i = 0 ; i <= 25;i++)
       if(a[i] == T)
        nCount++;

    cout<<nCount;
    return 0;
}

Python

import sys

def gemstones(arrays):
    superset = set(arrays[0])
    
    for arr in arrays[1:]:
        superset &= set(arr)
    
    return len(superset)

n = int(input().strip())
arr = []
arr_i = 0
for arr_i in range(n):
    arr_t = str(input().strip())
    arr.append(arr_t)
result = gemstones(arr)
print(result)

Java

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        input.nextLine();
        
        Set<Character> gemstones = stringToSet(input.nextLine()); //Set of gemstones
        
        for(int i=1; i<n ;i++){
            gemstones.retainAll(stringToSet(input.nextLine())); //Perform intersection
        }
        System.out.print(gemstones.size());
    }
    
    public static Set<Character> stringToSet(String s) //Converts String to Character set
    {
        Set<Character> set = new HashSet<Character>(26);
        for (char c : s.toCharArray())
            set.add(Character.valueOf(c));
        return set;
    }
}

Note: This problem (Gemstones) is generated by HackerRank but the solution is provided by Codingbroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *