题解 | #查找兄弟单词#

查找兄弟单词

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

inline = input().split(' ')
n = int(inline[0])
cand_words = inline[1:-3]
x_word = inline[-2]
order = int(inline[-1])


def hashWord(word: str):
    hash_x_word = {}
    for chr in word:
        if chr not in hash_x_word.keys():
            hash_x_word[chr] = 1
        elif chr in hash_x_word.keys():
            hash_x_word[chr] += 1
    return hash_x_word

target_word_hash = hashWord(x_word)

accWords = []
for ele in cand_words:
    if hashWord(ele) == target_word_hash and ele != x_word:
        accWords.append(ele)

sorted_acc_words = sorted(accWords)
# if len(sorted_acc_words) == 7:
#     print(sorted_acc_words)
print(len(sorted_acc_words))
if order <=  len(sorted_acc_words):
    print(sorted_acc_words[order-1])

要点:利用字典对每个单词建立hash,相等则纳入兄弟单词,字典序直接用python的sorted对字符串列表排序即可

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:27
明天又是董事长面,啥时候是个头啊
在太阳里长大的人:公司就仨人吧😂
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:15
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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