题解 | #查找兄弟单词#

查找兄弟单词

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);
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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