题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String input = in.nextLine();
        String[] a = input.split(" ");
        int num = Integer.parseInt(a[0]);
        String x = a[num + 1];
        int count = 0;
        int k = Integer.parseInt(a[a.length - 1]);
        List<String> clist = new ArrayList<>();
        char[] xchar = x.toCharArray();
        for (int i=1;i < 1+num;i++){
             Map<Character,Integer> map = new HashMap<>();
             for (char n : xchar){
                int y =1;
                 if (map.get(n)!= null){
                     y = map.get(n) + 1;
                     map.put(n,y);
                 } else {
                     map.put(n,y);
                 }

             }
            if (a[i].equals(x)){
                continue;
            }
            if (a[i].length() != x.length()){
                continue;
            }
            char[] achar = a[i].toCharArray();
            for (char j : achar){
                int flag = 0;
                if (map.get(j) != null && map.get(j) !=0){
                    flag = map.get(j);
                    flag--;
                    map.put(j,flag);
                } else {
                    break;
                }
            }
            if (map.values().stream().allMatch(integer -> integer==0)) {
                clist.add(a[i]);
                count++;
            }

        }
        List<String> mlist = clist.stream().sorted(String::compareTo).collect(Collectors.toList());
        if (k < mlist.size()) {
            System.out.println(count + "\n" + mlist.get(k - 1));
        } else {
            System.out.println(count + "\n");
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
06-27 18:45
中山大学 Ruby
25届应届毕业生,来广州2个礼拜了,找不到工作,绝望了,太难过了…
应届想染班味:9爷找不到工作只能说明,太摆了或者太挑了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
字节一直是我的白月光,考虑到转正还是拒了日常实习。
从明天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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