Simple Java solution O(n) and runs in 16ms


  • 0
    A
    public static int firstUniqChar(String s) {
    	int count_array[] = new int[26];
    	char string_array[] = s.toCharArray();
    
        for(int i = 0;i<string_array.length;i++){
        	char current_char = string_array[i];
        	if(count_array[current_char-'a']==0)
        		count_array[current_char-'a'] =1;
        	else
        		count_array[current_char-'a'] = count_array[current_char-'a']+1;
        }
        	
    
       
       for(int i = 0;i<string_array.length;i++){
    	   char current_char = string_array[i];
    	   if(count_array[current_char-'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.