9 ms solution using hash table


  • 0
    int firstUniqChar(char* s) {
        int hash[30] = {0};
        int len = strlen(s);
        int i;
        for(i = 0; i < len; ++i)
            hash[s[i]-'a']++;
        for(i = 0; i < len; ++i)
            if(1 == hash[s[i]-'a'])
                break;
        if(i == len) return -1;
        return i;
    }
    

Log in to reply
 

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