题目

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。


题解

感觉没啥好写,遍历统计就是了

code

  1. class Solution:
  2. def firstUniqChar(self, s: str) -> int:
  3. countCh = collections.Counter(s)
  4. for i, ch in enumerate(s):
  5. if countCh[ch] == 1:
  6. return i
  7. return -1