题解 | #查找兄弟单词#
查找兄弟单词
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])
查看7道真题和解析