题解 | 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;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:00
林子大了什么鸟都有啊,我觉得我说的已经很客气了,阴阳谁呢
牛客62656195...:应该不是阴阳吧?你第一次注册的时候boss就说你是牛人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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