对于一个字符串,请设计一个高效算法,找到第一次重复出现的字符。
给定一个字符串(不一定全为字母)A及它的长度n。请返回第一个重复出现的字符。保证字符串中有重复字符,字符串的长度小于等于500。
测试样例:
"qywyer23tdd",11
返回:y
webary
class FirstRepeat:
def findFirstRepeat(self, A, n):
# write code here
y=[]
for i in A:
if i not in y:
y.append(i)
else :
return i
class FirstRepeat:
def findFirstRepeat(self, A, n):
# write code here
table = {}
for i in A:
if not i in table:
table[i] = 0
else:
return i
break