211

问答题 211 /413

手写代码:给一个字符串找出第一个只出现一次的字符位置

参考答案

参考回答:

def first_not_repeating_char(string):
if not string:
return -1
resultDict = {}
for k, s in enumerate(string):
resultDict [s] = [resultDict [s][0] + 1,k] if resultDict .get(s) else [1,k]
pos = len(string)
ret = None
for x in resultDict :
if resultDict [x][0] ==1 and resultDict [x][1] <pos:
pos = resultDict [x][1]
ret = (x,pos)
return ret