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

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

https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

import sys
from collections import OrderedDict

# 采用字典类型对每个字符进行计数。
#   dict类型当前也具有保持顺序的特性,
#   但此处有意强调,采用OrderedDict。
char_count = OrderedDict()
for char in sys.stdin.read().strip():
    char_count[char] = (char_count[char] + 1
            if char in char_count
            else 1)

for char, count in char_count.items():
    if count == 1:
        print(char)
        break
else:
    print(-1)

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务