I am trying to understand, what is the best way to answer this question. My attempt is below and I am able to pass all test cases but i'm not sure if this is the best way to code the answer and most time and space efficient.
Also, i have used an OrderedDict() since I am trying to return the first character. Is there any tradeoff to using the OrderedDict() as opposed to the dict() in this question specifically? Previously, I tried this question using the dict() and it still was able to pass all test cases but i'm thinking this will not always be the case since the items in this dictionary would be unordered?
Also, is the time complexity of this solution below O(n) since we are iterating through each char of the string s?
def firstNotRepeatingCharacter(s): d1=OrderedDict() for i in s: if i not in d1: d1[i]=0 if i in d1: d1[i]=d1[i]+1 for x, y in d1.items(): if y==1: return x return "_"
ifby just doingn=di.get(i, 0)and then in the next line justdi[i]=n+1