【题目】
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
【示例】
s = “abaccdeff”
返回 “b”
s = “”
返回 " "
【限制】
0 <= s 的长度 <= 50000
【代码】
【Python】
执行用时:
72 ms, 在所有 Python3 提交中击败了96.48%的用户
内存消耗:
15 MB, 在所有 Python3 提交中击败了33.79%的用户
class Solution:
def firstUniqChar(self, s: str) -> str:
cnt=dict(Counter(s))
for k in cnt:
if cnt[k]==1:
return k
return " "
【方法2】
class Solution:
def firstUniqChar(self, s: str) -> str:
cnt=dict()
for x in s:
cnt[x]=cnt.setdefault(x,0)+1
for x in cnt:
if cnt[x]==1:
return x
return " "