13 lines simple c solution


  • 0
    S

    O(n) for time and const space.

    int firstUniqChar(char* s) {
        unsigned int bucket[255]={0};
        int i = 0;
        for(i=0;i<strlen(s);i++){
            bucket[s[i]]++;
        }
        for(i=0;i<strlen(s);i++){
            if(bucket[s[i]]==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.