My Solution


  • 0
    V

    class Solution {
    public:
    int firstUniqChar(string s) {
    unordered_map<char,int> m;
    size_t n;
    for(int i=0;i<s.size();i++)
    { if(m.find(s[i])==m.end())
    { n=count(s.begin(),s.end(),s[i]);
    m[s[i]]=n;
    if(n==1)
    return i;
    }
    }
    return -1;
    }

    };
    Does not exceed the run time if you check beforehand whether key value pair exists in map or not. Otherwise i will give TLE


Log in to reply
 

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