题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

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.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;
        StringBuilder sb = new StringBuilder();
        DecimalFormat df = new DecimalFormat("#0.0");
        int parseInt = Integer.parseInt(bf.readLine());
        int[] count = new int[parseInt];
        int j = 0;
        while ((str = bf.readLine()) != null) {
            char[] chars = str.toCharArray();
            TreeMap<String, Integer> hashMap = new TreeMap<>();
            for (char aChar : chars) {
                String s = aChar + "";
                if (hashMap.containsKey(s)) {
                    Integer integer = hashMap.get(s);
                    integer++;
                    hashMap.put(s, integer);
                } else {
                    hashMap.put(s, 1);
                }
            }
            Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
            ArrayList<Map.Entry<String, Integer>> al = new ArrayList<>(entries);
            Collections.sort(al, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1,
                                   Map.Entry<String, Integer> o2) {
                    int i = o2.getValue() - o1.getValue();
                    return i;
                }
            });

            int i = 26;
            for (Map.Entry<String, Integer> entry : al) {
                count[j] = count[j] + entry.getValue() * (i--);
            }
            j++;
        }
        Arrays.stream(count).forEach(System.out::println);
    }


}

全部评论

相关推荐

01-02 16:50
门头沟学院 Java
不放弃的小鱼干很洒脱:比kpi面面完了也不发感谢信全靠自己猜的好多了
点赞 评论 收藏
分享
2024-11-21 23:29
已编辑
湖南涉外经济学院 前端工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务