LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
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;
  }
};