第一个只出现一次的字符
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
思路
- 词典。
记录char和次数的词典。
代码
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
countDict = {}
for char in s:
countDict[char] = countDict.get(char,0) + 1
for i in range(len(s)):
if countDict[s[i]] == 1:
return i
else:
return -1