Unique character lookup I'm solving 387. First Unique Character in a String LeetCode problem defined as:


  Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
  
  Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

  
  Note: You may assume the string contain only lowercase letters.


Taking advantage of the input being fully lowercase ASCII I created two bit vectors to track when we encounter a character for the first and second time. 

Can this code be improved further? LeetCode says that below code is better than 94.33% solutions. What else could have been done by the last 5.67% solutions that they were better?

class Solution {

  public int firstUniqChar(String s) {
    int firstSpot = 0;
    int secondSpot = 0;
    char[] chars = s.toCharArray();

    for (char c : chars) {
      int mask = 1 &lt;&lt; c - 'a';
      if ((firstSpot &amp; mask) == 0) {
        firstSpot |= mask;
      } else if ((secondSpot &amp; mask) == 0) {
        secondSpot |= mask;
      }
    }

    int i = 0;
    for (char c : chars) {
      int mask = 1 &lt;&lt; c - 'a';
      if ((secondSpot &amp; mask) == 0) {
         return i;
      }
      i++;
    }
    return -1;
  }

}




Are there tricks that can be done to improve the LeetCode score? It seems that enhanced for-each loop performs better than standard for loop but I can't prove it. It's an observation based on few of my previous submissions.
When in doubt, profile. We may theorize about possible optimizations, but only the profiler can definitely say where the program does spend time.
That said, 


the first/second spot logic looks suboptimal. Consider

if ((firstSpot &amp; mask) != 0) {
    secondSpot |= mask;
} else {
    firstSpot |= mask;
}



(using else may also be redundant).


It is unclear (to me) how for (char c : chars) over the char array fares against other techniques. This could be an entertaining reading.


Yet again, profile.
Strictly speaking, the challenge only says the string contain only lowercase letters, but does not specify the locale.