387. 字符串中的第一个唯一字符
题解:
- 首先,我们需要遍历字符串s,统计每个字符出现的次数。
- 然后,我们再次遍历字符串s,找到第一个只出现一次的字符,返回它的索引。
- 如果遍历完字符串s都没有找到只出现一次的字符,那么返回-1。
class Solution:def firstUniqChar(self, s: str) -> int:count = {}for c in s:count[c] = count.get(c, 0) + 1for i, char in enumerate(s):if count.get(char) == 1:return ireturn -1