Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

1
2
3
4
5
s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int firstUniqChar(string s) {
int result = -1, len = s.length();
if (len == 0)
return -1;
if (len == 1)
return 0;
vector<int> count(26);
for (int i = 0; i < len; i++) {
count[s[i] - 'a']++;
}

for (int i = 0; i < len; i++) {
if (count[s[i] - 'a'] == 1) {
result = i;
break;
}
}

return result;
}
};