字符串中找到第一个只出现一次的字符

'''
在一个长为a字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
'''
class Solution:
def FirstNotRepeatingChar(self, s):
temp = OrderedDict()
for i in s:
if i not in temp:
temp[i] = 1
else:
temp[i] += 1

for j in temp:
if temp[j] == 1:
return s.index(j)
else:
return -1

if __name__ == "_main__":
s = 'aa'
print(Solution.FirstNotRepeatingChar(self, s))
posted @ 2022-01-28 16:25  keep2021  阅读(106)  评论(0编辑  收藏  举报