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

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

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)

全部评论

相关推荐

点赞 评论 收藏
分享
叁六玖:不买课还想秋招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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