dp

abb

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

import java.io.*;

public class Main {
    static int[] word = new int[26];
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static String[] lines;

    public static void main(String[] args) throws IOException {
        lines = in.readLine().split(" ");
        int n = Integer.parseInt(lines[0]);
        lines = in.readLine().split(" ");
        String s = lines[0];
        int length = s.length();
        long sum = 0;
        for (int i = length - 1; i >= 0; i--) {
            char temp = s.charAt(i);
            int index = temp - 'a';
            for (int j = 0; j < 26; j++) {
                if (j != index && word[j] >= 2) {
                    sum += (long) word[j] *(word[j]-1)/2;
                }
            }
            word[index]++;
        }
        System.out.println(sum);
    }
}

全部评论

相关推荐

看网上风评也太差了
投递万得信息等公司9个岗位 >
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务