leetcode 387. First Unique Character in a String

Orignal question can be found here.

My solution is as follows:

int firstUniqChar(char* s)
{
        unsigned int hash[26];

        for(int i=0; i<26; ++i)
                hash[i]=0;

        for(int i=0; i<strlen(s); ++i)
        {
                ++hash[(s[i] - 'a')];
        }

        for(int i=0; i<strlen(s); ++i)
                if(hash[(s[i] - 'a')] == 1)
                        return i;
        return -1;
}

Leave a Reply