题目
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
解题思路
将字符串转换成计数器,从头开始遍历字符串找到第一个只出现一次的字母,找不到返回-1
代码
from collections import Counter
class Solution:
def firstUniqChar(self, s: str) -> int:
ct = Counter(s)
for i, c in enumerate(s):
if ct[c] == 1:
return i
return -1