题解 | abb

abb

https://www.nowcoder.com/practice/0a8bbf8b9b5b4280957849ef4f240f07

import java.util.*;

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

    private static long getCount(String s) {
        long count = 0;
        HashMap<Character, Long> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0L) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            Iterator<Map.Entry<Character, Long>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Character, Long> next = iterator.next();
                char key = next.getKey();
                long value = next.getValue();
                if (key != ch && value >= 2) {
                    count += (value*(value-1)/2);
                }
            }
            map.put(ch, map.get(ch) - 1);
        }
        return count;
    }
}

全部评论

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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