First Unique Character in a String

2017/12/11 posted in  leetcode

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char, int> map;
        for (int i =0; i<s.length(); i++) {
            map[s[i]]++;
        }
        for (int i =0; i<s.length(); i++) {
            if (map[s[i]]==1) {
                return i;
            }
        }
        return -1;
    }
};