HackerRank Gemstones problem solution YASH PAL, 31 July 2024 In this HackerRank Gemstones problem you have Given a list of minerals embedded in each of the rocks, display the number of types of gemstones in the collection. Problem solution in Python programming. import fileinput stones = [] gems = [] for line in fileinput.input(): if fileinput.lineno() == 1: lengths = int(line) else: stones.append(line.strip()) #print(stones) for x in range(0, len(stones[0])): if stones[0][x] not in gems: gems.append(stones[0][x]) for y in range(0, lengths): if stones[0][x] not in stones[y]: #print(x) try : gems.remove(stones[0][x]) except ValueError: continue print(len(gems)) Problem solution in Java Programming. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int gemStones(ArrayList<String> array) { String firstString = array.get(0); // charset with all unique chars HashSet<String> charSet = new HashSet<String>(); for (String c : firstString.split("")) { charSet.add(c); } int total = 0; for (String c : charSet) { Boolean exists = true; for (int i = 1 ; i < array.size() ; i++) { String thisLine = array.get(i); if (!thisLine.contains(c)) { exists = false; } } if (exists) total++; } return total; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = in.nextInt(); // escape the n in.nextLine(); ArrayList <String> arrayList = new ArrayList<String>(count); for (int i=0;i<count;i++) { String line = in.nextLine(); arrayList.add(line); } in.close(); int result = gemStones(arrayList); System.out.println(result); } } Problem solution in C++ programming. #include <cmath> #include <ctime> #include <cstdio> #include <cstdlib> #include <cstring> #include <cassert> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #define DB(x) cerr << x << " " #define DBN(x) cerr << #x << "=" << x << endl #define DBL cerr << endl #define sz(c) ((int)(c).size()) #define pb push_back #define mp make_pair #define endl 'n' typedef long long int64; using namespace std; int n; int cnt[26]; int main() { while (scanf("%d", &n) == 1) { memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; ++i) { char s[110]; scanf("%s", s); set<int> was; for (int i = 0; s[i] != 0; ++i) if (!was.count(s[i])) { ++cnt[s[i] - 'a']; was.insert(s[i]); } } int res = 0; for (int i = 0; i < 26; ++i) if (cnt[i] == n) ++res; printf("%dn", res); } return 0; } Problem solution in C programming. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #define NL ('z' - 'a' + 1) int main(int argc, char *argv[]) { if (argc > 1) freopen(argv[1], "r", stdin); //Helpers for input/output int N; int total[NL] = {0}; int part[NL]; unsigned char elem[101]; int result = 0; scanf("%dn", &N); for(int i=0; i<N; i++) { memset(part, 0, NL * sizeof(int)); scanf("%sn", elem); for (unsigned char *c = elem; *c; c++) { if (!part[*c - 'a']) { part[*c - 'a']++; total[*c - 'a']++; } } } for(int i=0; i<NL; i++) { if (total[i] == N) result++; } printf("%dn", result); return 0; } Problem solution in JavaScript programming. 'use strict'; function processData(input) { var lines = input.split('n').slice(1); var gemElements = lines.reduce(function(prev, curr, index, array) { var found = []; var letters = curr.split('').map(function(letter){ if(prev.indexOf(letter) !== -1 && found.indexOf(letter) === -1) { found.push(letter); return letter; } }); return letters.join(''); }); console.log(gemElements.length); } process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); algorithm coding problems