# 387. First Unique Character in a String
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.
# Solution
Approach 1: two passes using a hashtable.
# Code (Python)
Approach 1:
class Solution:
def firstUniqChar(self, s: str) -> int:
if not s:
return -1
chars = {}
for char in s:
if char not in chars:
chars[char] = 0
chars[char] += 1
for index, letter in enumerate(s):
if chars[letter] == 1:
return index
return -1
# Code (C++)
Approach 1:
Approach 2: