aboutsummaryrefslogtreecommitdiff
path: root/first_unique_char_string.cpp
blob: 5f5582170f779ed32d38d69efb99e86c6fb235d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "leetcode.h"

class Solution {
public:
  int firstUniqChar(string s) {
    unordered_map<char, pair<int, int>> ump;
    int idx = s.size();
    for (int i = 0; i < s.size(); i++) {
      ump[s[i]].first++;
      ump[s[i]].second = i;
    }
    for (const auto &[c, p] : ump)
      if (p.first == 1)
        idx = min(idx, p.second);
    return idx == s.size() ? -1 : idx;
  }
};

int main() {
  Solution obj;
  cout << obj.firstUniqueChar("loveleetcode");
}