看题发现就是找一个小写字母后面与他不同的小写字母每一种的个数再求Cn2累加。
时间复杂度On因为只有26个小写字母
static long jg = 0; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int n = nextint(); String x = next(); HashMap<Character, Long> h = new HashMap<>(); for (int i = n - 1; i > -1; i--) { char c = x.charAt(i); h.forEach((o, p) -> { if (o != c && p >= 2) { jg += ((p) * (p - 1)) / 2; } }); h.put(c, h.getOrDefault(c, 0l) + 1); } println(jg); flsh(); } //省略io流输入
#牛客春招刷题训练营#https://www.nowcoder.com/creation/subject/d90225fecf404712be5150c470bffc45