题解 | #查找兄弟单词#

查找兄弟单词

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对字符串列表排序即可

全部评论

相关推荐

02-23 19:06
已编辑
西北工业大学 Java
点赞 评论 收藏
分享
02-25 16:55
已编辑
北京工业大学 Java
211本,找日常实习的话,如果面向中厂的话,需要刷hot100么?因为之前从来没刷过,算法仅限于学校课程水平,准备3月投递简历,现在还需要背八股文,时间有些紧张,还需要刷算法题么?同时什么样的公司可以算是中厂呢?
程序员小白条:中大厂说的上名字的,必定要算法,hot100只是最基础的了,题库远不止100题捏,一般在300-400题量之间,算法=学校课程=简单题也做不出,多准备八股文和算法吧,其他项目可以放放,精刷算法就行了,花时间成长很快的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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