Simple Java Solution 18ms beats 93.7%


  • 0
    E
    public class Solution {
        public int firstUniqChar(String s) {
            int[] count = new int[26];
            char[] str = s.toCharArray();
            int min = Integer.MAX_VALUE;
            for(int i = 0;i<str.length;i++){
                int index = str[i] - 'a';
                if(count[index]>0) count[index] = -1;
                else if(count[index]==0) count[index] = i + 1;
            }
            for(int i : count){
                if(i!=-1 && i!=0) min = Math.min(i,min);
            }
            return min==Integer.MAX_VALUE?-1:min - 1;
        }
    }
    

Log in to reply
 

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