Leetcode 387 Solution

This article provides solution to leetcode question 387 (first-unique-character-in-a-string)

https://leetcode.com/problems/first-unique-character-in-a-string

Solution

class Solution {
public:
    int firstUniqChar(string s) {
        map<char, int> a;

        for (auto it = s.begin(); it != s.end(); it++)
            a[*it]++;

        for (int i = 0; i < s.size(); i++)
            if (a[s[i]] == 1)
                return i;

        return -1;
    }
};