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.
Companies:
Facebook, Bloomberg, Amazon, Apple, Zillow, Goldman Sachs, Microsoft, Google, Yahoo
Related Topics:
Hash Table, String
Similar Questions:
// OJ: https://leetcode.com/problems/first-unique-character-in-a-string/
// Author: github.com/lzl124631x
// Time: O(S)
// Space: O(1)
class Solution {
public:
int firstUniqChar(string s) {
int cnt[26] = {0};
for (char c : s) cnt[c - 'a']++;
for (int i = 0; i < s.size(); ++i) {
if (cnt[s[i] - 'a'] == 1) return i;
}
return -1;
}
};