Since there was some discussion to provide an optimized solution, here it is:
This build's on @SowjanyaRBhat 's code and optimizes it. Pretty much what @OneCriketeer mentioned.
Space Complexity : O(N)
Time Complexity : O(N)
Approach 1: without collections.Counter
from collections import defaultdict
def getOddOccurrence(myString):
count = defaultdict(int)
for letter in myString:
count[letter] += 1
for letter in myString:
if count[letter] % 2 != 0:
return letter
Approach 2: with collections.Counter
(check the cool one-liner in @Alain.T's answer)
from collections import Counter
def getOddOccurrence(myString):
count = Counter(myString)
for letter in myString:
if count[letter] % 2 != 0:
return letter
# driver code
myString = "llo world!"
print(getOddOccurrence(myString))
# Output: "l"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…