C Solution 9ms


  • 0
    A
    int firstUniqChar(char* s) {
        size_t count[26] = {0};
        size_t index[26];
        
        for (size_t i = 0; s[i]; ++i) {
            // Increment count, and check first appearance. If yes, store the index.
            if (++count[s[i] - 'a'] == 1) {
                index[s[i] - 'a'] = i;
            }
        }
        
        int retVal = -1;
        
        for (int i = 0; i < 26; ++i) {
            if (count[i] == 1) {
                if ((retVal > index[i]) || (retVal == -1)) {
                    retVal = (int)index[i];
                }
            }
        }
        
        return retVal;
    }
    

Log in to reply
 

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