题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

# 存储x中字符个数
import copy

x_dict = {}

# 存储所有找到的兄弟节点
sbling_list = []

m_n, x, k = input().rsplit(" ", 2)
n = m_n.split(" ")[1:]

# 初始化x_dict字典
for ch in x:
    if x_dict.get(ch):
        x_dict[ch] += 1
    else:
        x_dict[ch] = 1

def is_sbling(astr:str):
    if astr == x:
        return False
    tmp_dict = copy.deepcopy(x_dict)
    for ch in astr:
        ch_count = tmp_dict.get(ch)
        if ch_count and ch_count > 0:
            tmp_dict[ch] -= 1
        else:
            return False
    if sum(tmp_dict.values()) == 0:
        return True

for word in n:
    if is_sbling(word):
        sbling_list.append(word)

sbling_list.sort()
sbling_len = len(sbling_list)
print(sbling_len)
if sbling_len >= eval(k):
    print(sbling_list[eval(k)-1])

全部评论

相关推荐

不愿透露姓名的神秘牛友
09-05 16:30
投递中兴通讯等公司10个岗位
点赞 评论 收藏
分享
07-19 13:28
长沙学院 Java
鸿哥鸿哥:学院(一本),感觉在脱ku子放屁,学院结尾的除了那几家出名的,一律按二本处理
点赞 评论 收藏
分享
码不完,根本码不完啊。。。
投递4399游戏等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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