I'm trying to solve the firstNotRepeatingCharacter question on CodeSignal. The problem is that given a string of characters (in this case s), I need to return the first non-duplicate character. I don't understand why my code isn't working.
I'll paste the link to the problem although you'll probably need to sign up.
def solution(s):
# run through all elements
for i,el in enumerate(s):
# helps check what's going on
#print(i,el,s[i+1:], s[i-1::-1])
# if it's the first element, only check for duplicates in characters in front of it
# if no duplicate present, return first element
if i == 0:
if el not in s[i+1:]:
return i,el
# if it's not the first element, check for duplicates in characters in front of it and behind it
# if no duplicate is present, return that element
elif el not in ( s[i+1:] or s[i-1::-1] ):
return i,el
#otherwise, return '_'
return '_'
s='habahzb'
solution(s)
expected output z, output a
I have tried:
elif (el not in s[i+1:]) or (el not in s[i-1::-1]):
But I run into a similar issue.