题解 | #查找兄弟单词#

查找兄弟单词

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

public class Main27 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String line = in.nextLine();
            String[] strs = line.split(" ");
            int val = Integer.valueOf(strs[0]);
            String x = strs[strs.length - 2];
            int k = Integer.valueOf(strs[strs.length - 1]);
            ArrayList<String> list = new ArrayList<>();
            for (int i = 1; i <= val; i++) {
                if(isBrother(x,strs[i])){
                    list.add(strs[i]);
                }
            }
            System.out.println(list.size());
            if (list.size() >= k) {
                Collections.sort(list);
                System.out.println(list.get(k-1));
            }
        }
    }

    private static boolean isBrother(String x, String y) {
        if (x.length() != y.length() || x.equals(y)) {
            return false;
        }
        char[] charsX = x.toCharArray();
        char[] charsY = y.toCharArray();
        Arrays.sort(charsX);
        Arrays.sort(charsY);
        return new String(charsX).equals(new String(charsY));
    }
全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-12-12 19:18
wxg 开发 24Kx17 其他
不太迷人的反派_:得看背景,本科24,比我工作三年都高了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
2025-12-04 21:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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