本文最后更新于:2020年11月18日 下午

在字符串s中找出一个只出现一次的字符。如果没有,返回一个单空格。s只包含小写字母。

示例:

s = "abaccdeff"
返回"b"

s = ""
返回 " "

限制:

0 <= s的长度 <= 50000

class Solution:
	def firstUniqChar(self, s):

方法一:哈希表

class Solution:
	def firstUniqChar(self, s):
		dic = {}
		for c in s:
			dic[c] = not c in dic
		for c in s:
			if dic[c]: return c
		return ' '

方法二:有序哈希表

class Solution:
	def firstUniqChar(self, s):
		dic = collections.OrderedDict()
		for c in s:
			dic[c] = not c in dic
		for k,v in dic.items():
			if v: return k
		return " "