题解 | #合唱队#

查找兄弟单词

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

就是这么暴力....

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String[] arr = new String[n];
            // 3 abc bca cab abc 1
            for (int i = 0; i < n; i++) {
                arr[i] = in.next();
            }
            String x = in.next();
            int k = in.nextInt();
            String[] brotherWord = findBrother(x, arr);
            System.out.println(brotherWord.length);
            if (k - 1 < brotherWord.length) {
                System.out.println(brotherWord[k - 1]);
            }
        }
    }

    private static String[] findBrother(String x, String[] arr) {
        if (arr == null || arr.length == 0 || x == null) {
            return new String[0];
        }
        List<String> temp = new ArrayList<>();
        for (String s : arr) {
            if (!s.equals(x) && isBrother(s, x)) {
                temp.add(s);
            }
        }
        temp.sort(String::compareTo);
        temp.forEach(System.out::println);
        String[] ret = new String[temp.size()];
        temp.toArray(ret);
        return ret;

    }

    private static boolean isBrother(String s, String x) {
        if (s == null || x == null || s.length() != x.length()) {
            return false;
        }

        for (char c : s.toCharArray()) {
            if (!x.contains(c + "")) {
                return false;
            }
        }

        for (char c : x.toCharArray()) {
            if (!s.contains(c + "")) {
                return false;
            }
        }

        // 取出每一个字母的次数 不一样就不是
        Map<String, Integer> maps = new HashMap<>(s.length());

        for (char c : s.toCharArray()) {
            if (!maps.containsKey(c + "")) {
                maps.put(c + "", 1);
            } else {
                maps.put(c + "", maps.get(c + "") + 1);
            }
        }
        Map<String, Integer> mapx = new HashMap<>(x.length());
        for (char c : x.toCharArray()) {
            if (!mapx.containsKey(c + "")) {
                mapx.put(c + "", 1);
            } else {
                mapx.put(c + "", mapx.get(c + "") + 1);
            }
        }
        for (Map.Entry<String, Integer> entry:maps.entrySet()){
            if(!mapx.containsKey(entry.getKey()) || mapx.get(entry.getKey()).intValue()!=entry.getValue().intValue()){
                return false;
            }
        }
        for (Map.Entry<String, Integer> entry : mapx.entrySet()) {
            if(!maps.containsKey(entry.getKey()) || maps.get(entry.getKey()).intValue()!=entry.getValue().intValue()){
                return false;
            }
        }

        return true;
    }
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11036次浏览 94人参与
# 你的实习产出是真实的还是包装的? #
1950次浏览 42人参与
# 巨人网络春招 #
11365次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7640次浏览 43人参与
# 简历第一个项目做什么 #
31744次浏览 341人参与
# 重来一次,我还会选择这个专业吗 #
433544次浏览 3926人参与
# MiniMax求职进展汇总 #
24115次浏览 309人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187205次浏览 1122人参与
# 牛客AI文生图 #
21446次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152446次浏览 888人参与
# 研究所笔面经互助 #
118964次浏览 577人参与
# 简历中的项目经历要怎么写? #
310361次浏览 4219人参与
# AI时代,哪些岗位最容易被淘汰 #
63815次浏览 828人参与
# 面试紧张时你会有什么表现? #
30510次浏览 188人参与
# 你今年的平均薪资是多少? #
213134次浏览 1039人参与
# 你怎么看待AI面试 #
180131次浏览 1258人参与
# 高学历就一定能找到好工作吗? #
64331次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76539次浏览 374人参与
# 我的求职精神状态 #
448131次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363512次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160674次浏览 1112人参与
# 校招笔试 #
471179次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务