-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirstUniqChar.cpp
More file actions
39 lines (35 loc) · 792 Bytes
/
firstUniqChar.cpp
File metadata and controls
39 lines (35 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <limits>
// 2 pass solution
// 1 pass involves creatign map as <int, ITERATOR to a list>
using namespace std;
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, pair<int, bool>> map;
for (int i = 0; i < s.size(); i++) {
char c = s[i];
if (map.count()) {
map[c] = make_pair(i, false);
} else {
map[c] = make_pair(i, true);
}
}
int index = numeric_limits<int>::max();
for (auto i : map) {
if (i.second.second == true && i.second.first < index) {
index = i.second.first;
}
}
return (index == numeric_limits<int>::max()) ? -1 : index;
}
};
int main()
{
Solution s;
printf("%d \n", s.firstUniqChar("leetlove"));
return 0;
}