题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        int len = in.nextInt();
        String[] arr = new String[len];
        for (int i = 0; i < len; i++) {
            arr[i] = in.next();
        }
        String word = in.next();
        char[] wordChar = word.toCharArray();
        Arrays.sort(wordChar);
        int index = in.nextInt();
        String k = "";
        int count = 0;
        Arrays.sort(arr);
        for (int i = 0; i < len; i++) {
            String str = arr[i];
            char[] strChar = str.toCharArray();
            Arrays.sort(strChar);

            if (!Arrays.equals(wordChar, strChar)) {
                continue;
            }
            if (word.equals(str)) {
                continue;
            }
            count++;
            if (count == index) {
                k = str;
            }
        }
        
        System.out.println(count);
        System.out.print(k);
    }
}

全部评论

相关推荐

纸鹰:对他说:“你好,我是百度JAVA。”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务