Java O(n) solution


  • 0
    A
        public int firstUniqChar(String s) {
            int[] lookup = new int[26];
            for(int i = 0 ; i < s.length(); i++){
                lookup[s.charAt(i) - 'a']++; 
            }
            for(int i = 0 ; i < s.length(); i++){
                if(lookup[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.