Simple 23ms 93% percentile solution


  • 0
    L
    public int firstUniqChar(String s) {
            if(s==null || s.length()==0) {
                return -1;
            }
            
            int[] arr = new int[26];
            Arrays.fill(arr,0);
            for(char c : s.toCharArray()) {
                arr[c-'a']++;
            }
            
            for(int i=0;i<s.length();i++) {
                if(arr[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.