题目要求
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
解题思路
字典
class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: str
"""
dic = {}
# 字典保存键值对,字符:出现次数。
for s_ in s:
if s_ not in dic:
dic[s_] = 1
else:
dic[s_] += 1
# 重新遍历s,找出第一个只出现一次的字符。
for s_ in s:
if s_ in dic and dic[s_] == 1:
return s_
# 否则返回' '
return ' '