题目
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
思路
用字典记录所有字符出现次数。
代码
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if not s: return -1
tmp_dict = {}
for c in s:
if c in tmp_dict: tmp_dict[c] += 1
else: tmp_dict[c] = 1
for i, c in enumerate(s):
if tmp_dict[c] == 1:
return i