Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
int firstUniqChar(string s) {
vector<int> v(26);
for (auto& x : s) {
++v[x - 'a'];
}
int res = INT_MAX;
for (int i = 0; i < size(v); ++i) {
if (v[i] == 1) {
int pos = s.find(i + 'a');
res = min(res, pos);
}
}
return res == INT_MAX ? -1 : res;
}
};