题解 | #查找兄弟单词#

查找兄弟单词

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

import sys

# 如果其余元素长度和待选单词一样,并且每个字母都落入单词中,那么count就+1
wordlist = list(map(str, input().split()))
key_word = wordlist[-2]
num_to_print = int(wordlist[-1])
count = 0
wordlist = wordlist[1:-1]
table = []
for word in wordlist:
    if len(word) == len(key_word):
        s = 1
        for i in range(len(word)):
            if sorted(word) == sorted(key_word):
                if key_word[i] == word[i]:
                    s *= 1
                else:
                    s *= 0
            else:
                s = 1
        if s == 0:
            count += 1
            table.append(word)
table = sorted(table)
if len(table)> num_to_print-1:
    print(count, table[num_to_print - 1], sep="\n")
else:
    print(len(table))

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务