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); } }