-
Notifications
You must be signed in to change notification settings - Fork 0
/
387.hpp
54 lines (42 loc) · 947 Bytes
/
387.hpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef LEETCODE_387_HPP
#define LEETCODE_387_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
/*
Given a string, find the first non-repeating character in it and return it's index.
If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
*/
class Solution {
public:
int firstUniqChar(string s) {
map<char, int> cnts;
for (int i = 0; i < s.size(); ++i) {
cnts[s[i]] += 1;
}
int idx = -1;
for (int i = 0; i < s.size(); ++i) {
if (cnts[s[i]] == 1) {
idx = i;
break;
}
}
return idx;
}
};
#endif //LEETCODE_387_HPP