题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
lst = input().strip().split(' ') #得到输入字符串以空格分开的列表
words = lst[1:int(lst[0])+1] #得到要处理的单词列表
index = int(lst[-1]) #得到目标索引
target = lst[-2] #得到目标单词
brothers = [] #构建兄弟单词空列表
for word in words: #遍历单词列表
if target != word and sorted(target) == sorted(word): #如果符合兄弟单词特征就加入兄弟单词列表
brothers.append(word)
brothers.sort() #对兄弟单词列表进行排序
print(len(brothers)) #输出符合单词列表的个数
try:
print(brothers[index-1]) #尝试输出单词列表的目标索引单词
except:
pass
查看3道真题和解析
