Diamond in the rough! Finding Gems in the rocks Recently a question was asked that solved a hackerrank problem: Counting gems amongst the rocks

The description of the challenge is:


  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 was in the process of writing an answer when the question was closed because the code in the question was failing to produce the right results.

My recommended solution to the problem is likely quite fast, but it is also relatively complicated. I am sure it can be improved, and simplified.

I have included a simple main method that shows how it can be used, and produces the sample output.

public class GemCounter {

    private static final int LETTERCOUNT = 26; 

    // Start with 1 bit set for each element/letter.
    private int gemsSoFar = (1 &lt;&lt; LETTERCOUNT) - 1; 

    public GemCounter() {
        // default constructor. Does nothing.
    }

    public void processRock(final String rock) {
        int gotElement = 0; // no bits are set.
        for (int i = 0; i &lt; rock.length(); i++) {
            int element = rock.charAt(i) - 'a';
            if (element &gt;= 0 &amp;&amp; element &lt; LETTERCOUNT) {
                // bitwise OR the bit that represents the element
                gotElement |= 1 &lt;&lt; element;
            }
        }
        // only bits that were found in this rock
        // and also that have been seen before
        // will remain set.
        gemsSoFar = gemsSoFar &amp; gotElement;
    }

    public int getGemCount() {
        // count the number of bits that are still set.
        return Integer.bitCount(gemsSoFar);
    }

    public static void main(String[] args) {
        String[] rocks = {"abcdde", "baccd", "eeabg"};

        GemCounter gemcount = new GemCounter();
        for (String rock : rocks) {
            gemcount.processRock(rock);
            System.out.printf("Processed %s, got %d gems still%n", rock, gemcount.getGemCount());
        }

        System.out.printf("%d rocks have %d gems%n", rocks.length, gemcount.getGemCount());

    }

}
You have added

public GemCounter() {
    // default constructor. Does nothing.
}


which is not needed for the program to work because,


  The compiler automatically provides a no-argument, default constructor
  for any class without constructors. This default constructor will call
  the no-argument constructor of the superclass. In this situation, the
  compiler will complain if the superclass doesn't have a no-argument
  constructor so you must verify that it does. If your class has no
  explicit superclass, then it has an implicit superclass of Object,
  which does have a no-argument constructor &#91;1&#93;.


therefore this also violates YAGNI


  "You aren't gonna need it" (acronym: YAGNI) is a principle of extreme
  programming (XP) that states a programmer should not add functionality
  until deemed necessary. XP co-founder Ron Jeffries has written:
  "Always implement things when you actually need them, never when you
  just foresee that you need them [2].



References


&#91;1&#93;“Providing Constructors for Your Classes.” [Online]. Available: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html. [Accessed: 27-Aug-2014].
&#91;2&#93;“You aren’t gonna need it,” Wikipedia, the free encyclopedia. 24-Aug-2014.