华为机试59- 找出字符串中第一个只出现一次的字符

题目描述
找出字符串中第一个只出现一次的字符

输入描述:
输入几个非空字符串

输出描述:
输出第一个只出现一次的字符,如果不存在输出-1

示例1
输入
asdfasdfo
aabb
输出
o
-1

 

参考:

defaultdict()函数

from collections import defaultdict
while(True):
    try:
        res = input()
        dic = defaultdict(int)   #关键
        n = len(res)
        for s in res:
            dic[s] += 1    #赋值
        a = 0
        for s in res:
            if dic[s] == 1:
                print(s)
                a = 1
                break
        if a == 0:
            print(-1)
    except:
        break
        

执行结果: 答案正确:恭喜!您提交的程序通过了所有的测试用例 用例通过率:100.00% 运行时间: 26ms 占用内存: 3676KB

posted @ 2020-08-22 09:28  Andy_George  阅读(128)  评论(0编辑  收藏  举报