# 【LeetCode】 387. First Unique Character in a String
## Description
> Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
> Note: You may assume the string contain only lowercase letters.
> 給予一個字串,找到第一個不重複的字元並回傳它的索引。如果不存在則回傳 -1。
## Example:
```
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
```
## Solution
* 最簡易的方法就是用一個hash去算每個字出現的字數。
* 這題應該可以壓在一個for loop就完成,但是要去紀錄目前最小的索引值,還要在字母出現第二次時去找上一個。
* 可以參考[這題](https://hackmd.io/@Zero871015/LeetCode-Other4),概念十分類似。
### Code
```C++=1
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, int> hash;
for(int i = 0; i < s.length(); i++)
{
hash[s[i]]++;
}
for(int i = 0; i < s.length(); i++)
{
if(hash[s[i]] == 1)
return i;
}
return -1;
}
};
```
###### tags: `LeetCode` `C++`