# 387. First Unique Character in a String

{% embed url="<https://leetcode.com/problems/first-unique-character-in-a-string/>" %}

{% tabs %}
{% tab title="Python" %}

```python
# python solution 1
class Solution:
    def firstUniqChar(self, s: str) -> int:
        index_count = 0
        repeat = []
        while s:
            if s[0] in repeat:
                pass
            elif s.count(s[0]) > 1:
                repeat.append(s[0])
            else:
                return index_count
            s = s[1:]
            index_count += 1
        return -1

# solution 2:
class Solution:
    def firstUniqChar(self, s: str) -> int:
        count = collections.Counter(s)
        for index, l in enumerate(s):
            if count[l] == 1:
                return index
        return -1
        
# solution 3 - O(1) space:
class Solution:
    def firstUniqChar(self, s: str) -> int:
        S = 'abcdefghijklmnopqrstuvwxyz'
        indices = [s.index(c) for c in S if s.count(c) == 1]
        return min(indices) if indices else -1
```

{% endtab %}

{% tab title="Java" %}

```java
// solution 1
class Solution {
    public int firstUniqChar(String s) {
        int[] count = new int[26];
        for (char c: s.toCharArray()) {
            count[c-'a']++;
        }
        for (int i=0; i<s.length(); i++) {
            if (count[s.charAt(i)-'a'] == 1) return i;
        }
        return -1;
    }
}

```

{% endtab %}
{% endtabs %}
