Topic describes
Find the first character in the string s that occurs only once. If not, a single space is returned. The s contains only lowercase letters. Example:
S = “abaccdeff” return “b”
S = “return “”
A Map B Map C Map D Map
Iterate over the string directly, marking each character true the first time it occurs and false the next time it occurs. At the end of the loop, characters marked true are characters that occur once. We iterate through the string again, checking the map in order to see if it is marked true, and get the first occurrence of the character
The sample code
def firstUniqChar(self, s: str) - >str:
map = {}
for i in s:
if i in map:
map[i] = False
else:
map[i] = True
for i in s:
if map[i] == True:
return i
return ""
Copy the code