题解 | #查找兄弟单词#

查找兄弟单词

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

while True:
    try:
        sr = input()
        sr_formate = sr.split(" ")
        x = sr_formate[int(sr_formate[0]) + 1]#找到x
        all_words = sr_formate[1:-2]
        x_list = []  #存放所有X的兄弟单词
        
        #兄弟单词判断逻辑,1.长度一致2.每个字母的数量一致3.不相等
        def is_xd(word, xd_word):
            
            w_len = len(word)
            xw_len = len(xd_word)
            if w_len == xw_len and word != xd_word:
                
                for i in [i for i in list(word)]:
                    if word.count(i) != xd_word.count(i):
                        return False
                return True
            else:
                return False
            
            #循环遍历是否为兄弟单词\
        for i in all_words:
            if is_xd(x, i):
                x_list.append(i)
        print(len(x_list))
        print(sorted(x_list)[int(sr_formate[-1])-1])
        
            
    except Exception as e:
        break

全部评论

相关推荐

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