题解 | #查找兄弟单词#

查找兄弟单词

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

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {
            int first = str.indexOf(" ");
            String[] s = str.substring(first).split(" ");
            String[] strings = stream(s).skip(1).toArray(String[]::new);
            //下标
            int pos = Integer.parseInt(strings[strings.length - 1]) - 1;
            String scale = strings[strings.length - 2];
            ArrayList<String> al = new ArrayList<>();
            char[] chars = scale.toCharArray();
            HashMap<Character, Integer>hashMap = new HashMap<>();
            for (int i = 0; i < chars.length; i++) {
                if (hashMap.containsKey(chars[i])) {
                    Integer integer = hashMap.get(chars[i]);
                    integer++;
                    hashMap.put(chars[i], integer);
                } else {
                    hashMap.put(chars[i], 1);
                }
            }
            for (int i = 0; i < strings.length - 2; i++) {
                if ( strings[i].length() != scale.length()) {
                    continue;
                }
                if (scale.equals(strings[i])) {
                    continue;
                }

                int flag = 0;
                for (int j = 0; j < scale.length(); j++) {
                    if (!strings[i].contains(chars[j] + "")) {
                        flag = 1;
                        break;
                    }
                    int py = strings[i].length() - strings[i].replaceAll(chars[j] + "",
                             "").length();
                    if (py != hashMap.get(chars[j])) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) {
                    al.add(strings[i]);
                }
            }
            Collections.sort(al);
            System.out.println(al.size());
           if (al.size()!=0&&al.size()>=pos){
                System.out.println(al.get(pos));
            }

        }





    }

}

全部评论

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务