题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.Scanner;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();

        List<String>  list = new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (number > 0) { // 注意 while 处理多个 case
            list.add(in.next());
            number--;
        }
        String a = in.next();
        int printRow = in.nextInt();
        List<String> bros = isBro(a, list);
        String[] array = bros.toArray(new String[0]);
        sort(array);

        System.out.println(bros.size());

        if (array.length > printRow && null != array[printRow - 1]) {
            System.out.println(array[printRow - 1]);
        }


    }

    private static void sort(String[] param) {

        Arrays.sort(param, new Comparator<String>() {

            public int compare(String a, String b) {
                int pos = 0;
                while (pos < a.length() || pos < b.length()) {

                    if (a.length() < pos + 1) {
                        return -1;
                    }
                    if (b.length() < pos + 1) {
                        return 1;
                    }
                    if (a.charAt(pos) == b.charAt(pos)) {
                        pos++;
                        continue;
                    }
                    return a.charAt(pos) > b.charAt(pos) ? 1 : -1;
                }

                return 0;
            }
        });
    }

    private static List<String> isBro(String a, List<String> param) {

        Map<Character, Integer> dic = new HashMap<>();
        List<Integer> counter = new ArrayList<>();
        int pos = 0;
        for (int i = 0; i < a.length(); i++) {
            char cha = a.charAt(i);
            if (null == dic.get(cha)) {
                dic.put(cha, pos);
                counter.add(1);
                pos++;
            } else {
                counter.set(dic.get(cha), counter.get(dic.get(cha)) + 1);
            }
        }

        List<String> res = new ArrayList<>();
        for (String b : param) {
            if (filter(a, b, counter, dic)) {
                res.add(b);
            }
        }
        return res;
    }

    private static boolean filter(String a, String b, List<Integer> counterA,
                                  Map<Character, Integer> dic) {
        List<Integer> counter = new ArrayList<>(counterA);
        if (a.length() != b.length()) {
            return false;
        }
        for (int i = 0; i < b.length(); i++) {
            char cha = b.charAt(i);
            if (null == dic.get(cha)) {

                return false;
            }
            int count = counter.get(dic.get(cha)) - 1;

            if (count < 0) {

                return false;
            }
            counter.set(dic.get(cha), count);
        }
        for (Integer count : counter) {
            if (count != 0) {

                return false;
            }
        }
        if (b.equals(a)) {

            return false;
        }
        return true;
    }
}

全部评论

相关推荐

10-29 16:42
门头沟学院 Java
1.今天什么国标的公司打电话约面试,还得准备ppt,好麻烦,网上查薪资一般,打算拒了,不面了2.字节又复活了,什么安全开发,也不知道怎么样,面一面试试吧,还是挺想去字节的,但好难,随缘吧所以今天没面试
嵌入式的小白:面试前可以好好准备下 1.看看你投递的岗位的岗位描述,分析下是哪个业务线,同使要罗列他们描述中提到的技术点 2.根据1中的两点准备 3.岗位描述中应该还有语言要求,这个刷刷八股,要是对自己语言能力很有把握,那就不用看这点了 4.找下你简历中项目部分,看有没有和岗位描述中技术点重合的,这种在面试提到项目时,是高概率问题 好好准备,祝你面试顺利
我的求职进度条
点赞 评论 收藏
分享
10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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