Java easy solution in 20 ms


  • 0
    Q
    	public int firstUniqChar(String s) {
    		if (s.length() == 0)
    			return -1;
    		if (s.length() == 1)
    			return 0;
    		char[] chars = s.toCharArray();
    		int[] words = new int[26];
    		Queue<Integer> list = new LinkedList<Integer>();
    		for (int i = 0; i < chars.length; i++) {
    			int w = chars[i] - 'a';
    			if (words[w] == 0) {
    				words[w] = i+1;
    				list.add(w);
    			} else if (words[w] > 0) {
    				words[w] = -1;
    				list.remove(w);
    			}
    		}
    		if (list.size() == 0)
    			return -1;
    		return words[(int) list.poll()]-1;
    
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.