## 题目描述

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.

## 解法一

### Python

``````class Solution:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
counts = [0] * 26
for item in s:
counts[ord(item) - ord('a')] += 1
for (index, item) in enumerate(s):
if counts[ord(item) - ord('a')] == 1:
return index
return -1``````

### Java

``````public class Solution {
public int firstUniqChar(String s) {
int[] counts = new int[26];
for (char item : s.toCharArray()) {
counts[item - 'a']++;
}
int index = 0;
for (char item : s.toCharArray()) {
if (counts[item - 'a'] == 1) {
return index;
}
index++;
}
return -1;
}
}``````

### C++

``````class Solution {
public:
int firstUniqChar(string s) {
vector<int> counts(26, 0);
for (auto item : s) {
counts[item - 'a']++;
}
for (int i = 0; i < s.size(); i++) {
if (counts[s[i] - 'a'] == 1) {
return i;
}
}
return -1;
}
};``````