题解 | #HJ027 查找兄弟单词#

查找兄弟单词

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

import java.util.*;

/**
 * HJ27 查找兄弟单词
 */
public class HJ027 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] ss = sc.nextLine().split(" ");
            Integer a = Integer.parseInt(ss[0]);
            String x = ss[ss.length - 2];
            Integer k = Integer.parseInt(ss[ss.length - 1]);
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= a; i++) {
                if (isBrother(x, ss[i])) {
                    list.add(ss[i]);
                }
            }
            int size = list.size();
            System.out.println(size);
            if (size >= k) {
                Collections.sort(list);
                System.out.println(list.get(k - 1));
            }
        }
        sc.close();
    }

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

相关推荐

魔法恐龙:这真得给个机会,面试的时候问问不吃饭78.5h怎么做到的
点赞 评论 收藏
分享
面了100年面试不知...:头像换成柯南再试试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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