Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
#Counter 计数器,顾名思义就是用来计数的,最主要的作用就是计算“可迭代序列中”各个元素(element)的数量
#list_01 = [1,9,9,5,0,8,0,9] #GNZ48-陈珂生日
#print(Counter(list_01)) #Counter({9: 3, 0: 2, 1: 1, 5: 1, 8: 1})
count = collections.Counter(s)
#enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中
#enumerate(sequence, [start=0]) sequence -- 一个序列、迭代器或其他支持迭代对象,start -- 下标起始位置
for idx, ch in enumerate(s):
if count[ch] == 1:
return idx
return -1
```