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[a-z]. 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 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 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.
Gemstones HackerRank Solution in C
#include<stdio.h> int main() { int n,i,j,freq[150][27]={0},count; char str[200]; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s\n",str); for(j=0;str[j]!='\0';j++) { freq[i][str[j]-97]++; } } count=0; for(i=0;i<26;i++) { for(j=0;j<n;j++) if(freq[j][i]>0) continue; else break; if(j==n) count++; } printf("%d\n",count); return 0; }
Gemstones HackerRank Solution in C++
#include <cstdio> #include <cmath> #include <iostream> #include <numeric> #include <algorithm> #include <string> #include <memory.h> #include <memory> #include <functional> #include <vector> #include <ctime> #include <cstdlib> #include <iomanip> #include <time.h> #include <map> #include <set> #include <climits> #include <queue> #include <sstream> #include <stack> #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <string> #include <fstream> #include <iterator> #include <cmath> #include <algorithm> #include <functional> #include <cstdio> #include <cstring> #include <vector> using namespace std; string s[101]; int main() { ios::sync_with_stdio(0); int n; cin >> n; for(int i = 0 ; i < n ; ++i) cin >> s[i]; int ans = 0; for(char ch = 'a' ; ch <= 'z' ; ++ch) { bool fl = 1; for(int i = 0 ; fl && i < n ; ++i) { fl = 0; for(int j = 0 ; j < s[i].size() ; ++j) if(s[i][j] == ch) fl = 1; } if(fl) ++ans; } cout << ans << "\n"; return 0; }
Gemstones HackerRank Solution in Java
from string import ascii_lowercase chars = ascii_lowercase n = input() R = [] c = 0 for i in range(n): R.append(raw_input()) for x in chars: present = True for r in R: if x not in r: present = False if present: c += 1 print c
Gemstones HackerRank Solution in Python
import java.util.Scanner; public class Solution { public static void main(String[] args) { int N; Scanner in = new Scanner(System.in); N = in.nextInt(); String s; int[] count = new int[26]; int[] temp; for (int i = 0; i < N; i++) { temp = new int[26]; s = in.next(); for (char c : s.toCharArray()) { temp[c - 97] += 1; if (temp[c - 97] == 1) count[c - 97] += 1; } temp = null; } int sum = 0; for (int i = 0; i < 26; i++) { if (count[i] == N) sum += 1; } count = null;//garabage collector System.out.println(sum); } }
Gemstones HackerRank Solution in C#
using System; using System.Collections.Generic; using System.IO; class Solution { static void doStuff(List<String> seed) { int counter = 0; String mainSeed = seed[0]; List<String> gems = new List<string>(); int _sampleLength = 1; while (_sampleLength <= mainSeed.Length) { int _sampleOffset = 0; while (_sampleOffset <= mainSeed.Length - _sampleLength) { String _sample = mainSeed.Substring(_sampleOffset, _sampleLength); if (!gems.Contains(_sample)) gems.Add(_sample); _sampleOffset++; } _sampleLength++; } foreach (string gem in gems) { if (hasGem(gem, seed)) counter++; } Console.WriteLine(counter); } static bool hasGem(string gem, List<string> seed) { for (int index = 1; index < seed.Count; index++) { if (seed[index].IndexOf(gem) == -1) return false; } return true; } static void Main(String[] args) { List<String> _seeds = new List<String>(); int _loops = Convert.ToInt32(Console.ReadLine()); for (int index = 0; index < _loops; index++) _seeds.Add(Console.ReadLine()); doStuff(_seeds); Console.ReadLine(); } }
Attempt Gemstones HackerRank Challenge
Link – https://www.hackerrank.com/challenges/gem-stones/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/alternating-characters-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.