-
Notifications
You must be signed in to change notification settings - Fork 856
/
Gemstones.java
59 lines (55 loc) · 1.38 KB
/
Gemstones.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
*
* Problem Statement-
* [Gemstones](https://www.hackerrank.com/challenges/gem-stones/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.strings;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Gemstones {
static int gemstones(String[] arr) {
int rockNum = 0;
int gemCount=0;
int[] compositions = new int[27];
int noOfRocks = arr.length;
for (rockNum= 1; rockNum <= noOfRocks; rockNum++) {
String rock = arr[rockNum-1];
storeComposition(compositions, rock, rockNum);
}
for(int element:compositions) {
if(element==noOfRocks)
gemCount++;
}
return gemCount;
}
/**
* @param compositions
* @param rock
*/
private static void storeComposition(int[] compositions, String rock, int rockNum) {
for (int i = 0; i < rock.length(); i++) {
int index = rock.charAt(i) - 'a';
if(rockNum==1) {
compositions[index]=1;
}else {
if (compositions[index] >= rockNum - 1)
compositions[index] = rockNum;
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] arr = new String[n];
for (int arr_i = 0; arr_i < n; arr_i++) {
arr[arr_i] = in.next();
}
int result = gemstones(arr);
System.out.println(result);
in.close();
}
}