题解 | #查找兄弟单词#

查找兄弟单词

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            List<String> list = new ArrayList<>();
            List<String> listN = new ArrayList<>();
            for (int i = 1; i <= n + 1; i++) {
                list.add(sc.next());
            }
            int k = sc.nextInt();

            String str = list.get(list.size() - 1);
            list.remove(list.size() - 1);
            String strN = getNew(str);
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals(str)) {
                    continue;
                }
                if (getNew(list.get(i)).equals(strN)) {
                    listN.add(list.get(i));
                }
            }
            Collections.sort(listN);
            System.out.println(listN.size());
            if (k <= listN.size()) {
                System.out.println(listN.get(k - 1));
            }else{
                continue;
            }
        }
    }

    private static String getNew(String str) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        //用作比较的str
        return String.valueOf(chars);
    }
}
全部评论

相关推荐

昨天 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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