java solution using bucket


  • 0
    J
    public int firstUniqChar(String s) {
            if(s==null || s.length()==0){
                return -1;
            }
            int size='z'-'a'+1;
            int[] bucket=new int[size];
            for(char c:s.toCharArray()){
                bucket[c-'a']++;
            }
            for(int i=0;i<s.length();i++){
                if(bucket[s.charAt(i)-'a']==1){
                    return i;
                }
            }
            return -1;
        }
    

Log in to reply
 

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