题解 | #找出字符串中第一个只出现一次的字符#

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

http://www.nowcoder.com/questionTerminal/e896d0f82f1246a3aa7b232ce38029d4

str_a=input()
str_a_new=list(set(str_a))
str_a_new.sort(key=lambda x:(str_a.count(x),str_a.index(x)))    #对这个列表排序,先根据出现次数统计排序,若统计次数相同则根据出现的索引排序
# print(str_a_new)
for i in str_a_new:
    if str_a.count(i)==1:                        #若是1则必然是第一个出现的,因为我们根据索引排过序了
        print(i)
        break
    if str_a.count(i)>1:
        print(-1)
        break
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务