找出字符流中第一个只出现一次的字符。例如,当从字符流google中只读出前两个字符go时,第一个只出现一次的字符是g;当读完google时,第一个只出现一次的字符是l。
class Solution():
def __init__(self):
#s存储读入字符流的字符
self.s=[]
#count存储字符出现的次数
self.count={}
def Insert(self,char):
self.s+=char
if char not in self.count:
self.count[char]=1
else:
self.count[char]+=1
def firstAppearingOnce(self):
for i in range(len(self.s)):
if self.count[self.s[i]]==1:
return self.s[i]
return '#'
s=Solution()
s.Insert('g')
s.Insert('o')
s.Insert('o')
s.Insert('g')
s.Insert('l')
s.Insert('e')
print(s.s)
print(s.count)
s.firstAppearingOnce()