v1
- 遍历n, 字典记录出现字符出现的次数和第一次出现的索引
时间O(n) + O(len(ret)) 空间O(n)
class Solution:
def firstUniqChar(self, s: str) -> int:
if s == "":
return -1
dic = {}
sorts = []
for idx, i in enumerate(s):
if i not in sorts:
sorts.append(i)
if dic.get(i):
dic[i][1] += 1
else:
dic[i] = [idx, 1]
for i in sorts:
if dic[i][1] == 1:
return dic[i][0]
return -1