#题目描述
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
题目链接
解题思路
先将所有字符出现的次数存入字典中,然后在对字符串从前往后遍历,找到第一个只出现一次的字符,并return其位置
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if len(s)<=0:return -1
dict = {}
for index in range(len(s)):
if s[index] not in dict:dict[s[index]] = 1
else:dict[s[index]] += 1
for index in range(len(s)):
if dict[s[index]] ==1:
return index
return -1