-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path387. First Unique Character in a String.cpp
38 lines (38 loc) · 1.31 KB
/
387. First Unique Character in a String.cpp
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
class Solution {
public:
int firstUniqChar(string s) {
// map<char, int> cache;
// for(auto && x : s){
// cache[x]++;
// }
// for(auto && x : s){
// if(cache[x] == 1){
// return distance(s.begin(), find(s.begin(), s.end(), x));
// }
// }
// return -1;
// -1 indicates that there are no occurences of char.
int count_chars[26] = {[0 ... 25] = -1};
for (int i=0; i<s.size(); i++) {
char c = s[i];
int val = count_chars[s[i] - 'a'];
if (val == -1) count_chars[c - 'a'] = i;
// -2 indicates that there are more than one occurence of char.
else if (val != -2) count_chars[c - 'a'] = -2;
}
int return_index = INT_MAX;
for (int i=0; i<26; i++) {
if (count_chars[i] >= 0) {
// Get the lowest index for a char that occurs only once.
return_index = min(return_index, count_chars[i]);
}
}
if (return_index != INT_MAX) return return_index;
return -1;
}
}
};