387. First Unique Character in a String
Problem¶
Given a string s
, find the first non-repeating character in it and return its index. If it does not exist, return -1
.
Solve¶
class Solution(object):
def firstUniqChar(self, s):
count = {}
pos = {}
for i, c in enumerate(s):
if c not in count:
count[c] = 0
pos[c] = i
count[c] += 1
possible = []
for k in count:
if count[k] == 1:
possible.append(pos[k])
if len(possible) == 0:
return -1
return min(possible)``
Last update :
October 13, 2023
Created : August 16, 2023
Created : August 16, 2023