2022年8月29日
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例 1:
输入:s = "abaccdeff"
输出:'b'
示例 2:
输入:s = ""
输出:' '
限制:
0 <= s 的长度 <= 50000

class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: str
"""
dic = {}
for ch in s:
if ch in dic.keys():
dic[ch] = False
else:
dic[ch] = True
for ch in s:
if dic[ch] == True:
return ch
return ' '
使用字典(哈希表)记录各字符是否重复即可。