John has discovered various rocks. Each rock is composed of various
elements, and each element is represented by a lowercase Latin letter
from 'a' to 'z'. An element can be present multiple times in a rock.
An element is called a 'gem-element' if it occurs at least once in
each of the rocks.

Given the list of rocks with their compositions, you have to print how
many different kinds of gems-elements he has.

Input Format

The first line consists of N, the number of rocks. Each of the next N
lines contain rocks’ composition. Each composition consists of small
alphabets of English language.

Output Format

Print the number of different kinds of gem-elements he has.

Constraints

\$1 ≤ N ≤ 100\$

Each composition consists of only small Latin letters ('a'-'z'). 1 ≤ Length of each composition ≤ 100

Sample Input

3
abcdde
baccd
eeabg

Sample Output

2

Explanation

Only "a", "b" are the two kind of gem-elements, since
these characters occur in each of the rocks’ composition.

I tried to solve this HackerRank problem in Java. Do you see any areas for improvement?

Here char c is going to be compare with even with first rock but since variable appearances is assigned as 0 then it will increment correctly. In my case I was using cnt =1 and comparing with next rock. I will improve myself for naming conventions.
–
VijayAug 27 '14 at 23:32

Yes, I've sacrificed a bit of efficiency for readability in the way the first string is processed. You could initialize appearances = 1 instead and start the inner for-loop at the second rock. The important point, though, is that appearances = 1 is only set at one place.
–
200_success♦Aug 27 '14 at 23:41

(note, that st.indexOf(c) is going to loop through the st string to find the char c).

Additionally, as has been pointed out, if the first rock has the same element multiple times, it will cause duplicate counting.

All together though, this creates an \$O(b \times i \times g)\$ complexity where b is the size of the first rock, i is the number of rocks, and g is the average length of each rock.

Removing the lists brings the process back to not having the indexOf, and replacing it with an indexed lookup (like an array, or bitset) that is indexed on the char value, will bring the problem back to just \$O(i \times g)\$

There is a trick that makes the system a whole lot faster....

Consider the fact that the input parameters for the challenge are well specified. The inputs are a to z characters. There are just 26 of them.

For brute performance, you can also rely on the fact that an int value in Java has 32 bits.... which is more than 26 letters.

You can prepare an int that has 1-bits set for each character...... and then use bitwise arithmetic to manipulate which elements are in each rock (the unique set), and then bitwise 'and' that with the gems you started with.

I was preparing this answer when this question was closed, so I posted a working implementation of my suggestions as a separate Code Review Question here. Now that this question is again reopened, I am completing my answer here too.

Sorry for the Java 8. I realized after writing it that it could be hard to read if you are not familiar with Java 8. The algorithm just intersects each set with the intersection of the previous sets until you get the intersection of all sets.

Your variable names are too short and un-Java-like. I suppose you are coming from another language than Java.

In my opinion, you have misinterpreted the challenge and therefore produced an incorrect solution. An element may appear more than once in a rock, but I believe that it should still only count once. Your code would double-count any element that appears twice in the first rock and that also appears in all subsequent rocks.