I read of a job interview question to write some code for the following:
Write an efficient function to find the first nonrepeated character in a st
In [1033]: def firstNonRep(word):
......: c = collections.Counter(word)
......: for char in word:
......: if c[char] == 1:
......: return char
......:
In [1034]: word="googlethis"
In [1035]: firstNonRep(word)
Out[1035]: 'l'
EDIT: If you want to implement the same thing without using helpers like Counter
:
def firstNonRep(word):
count = {}
for c in word:
if c not in count:
count[c] = 0
count[c] += 1
for c in word:
if count[c] == 1:
return c