class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, int> position;
queue<pair<char, int>> q;
int n = s.size();
for(int i = 0; i < n; ++i){
if(position.find(s[i]) == position.end()){
position[s[i]] = i;
q.push({s[i], i});
}else{
position[s[i]] = -1;
while(!q.empty() && position[q.front().first] == -1){
q.pop();
}
}
}
return q.empty() ? -1 : q.front().second;
}
};
08-03
550
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-04
660
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-05