---
# System prepended metadata

title: 'First Unique Character in a String <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# First Unique Character in a String <img src="https://img.shields.io/badge/-easy-brightgreen">

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. 

# Solution

```python=
class Solution:
    def firstUniqChar(self, s: str) -> int:
        d = {}
        for elt in s:
            if elt in d:
                d[elt] += 1
            else:
                d[elt] = 1
        for elt in d:
            if d[elt] == 1:
                return s.index(elt)
        return -1
```