题目:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
方法一:
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if s == "":
return -1
from collections import Counter
dict_ = Counter(s)
for i in s:
if dict_[i] == 1:
return s.find(i)
方法二:
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
if s == "":
return -1
for i in s:
if s.count(i) == 1:
return s.find(i)