题解 | #查找兄弟单词#

查找兄弟单词

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

相关推荐

Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务