646 · First Position Unique Character - LintCode

# Description

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

# Example

Example 1:

Input : s = "lintcode"
Output : 0

Example 2:

Input : s = "lovelintcode"
Output : 2

# Solution

public class Solution {
    /**
     * @param s: a string
     * @return: it's index
     */
    public int firstUniqChar(String s) {
        int[] count = new int[256];
        for(char c : s.toCharArray()){
            count[c]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if(count[s.charAt(i)] == 1){
                return i;
            }
        }
      return -1;
    }
}