第一次遍历:
建立哈希表,以单个字符Key,以字符出现次数Value
第二次遍历:
搜索出现次数为1的字符,返回位置;若查完都无,则返回-1

图片说明

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        res = {}
        for i in range(len(s)):
            if res.get(s[i]) == None:
                res[s[i]] = 1
            else:
                res[s[i]] += 1
        # print(res)
        for i in range(len(res)):
            if res[s[i]] == 1:
                # print(i)
                return i
        return -1