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

全部评论

相关推荐

牛客517626884号:嵌入式真难啊今年,我电赛国二都成了路边野狗了
点赞 评论 收藏
分享
认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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