题目描述
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
思路
代码
class Solution:
def firstUniqueChar(self, s:str)->str:
dic = {}
for c in s:
dic[c] = not c in dic
for c in s:
if dic[c]: return c
return ' '
时间复杂度 O(N): N为字符串 s 的长度;需遍历 s 两轮,使用 O(N) ;HashMap 查找的操作复杂度为 O(1) ;
空间复杂度 O(N) : HashMap 需存储 N 个字符的键值对,使用 O(N) 大小的额外空间。