Sum of Beauty of All Substrings
给一个string, 求它所有的substring的最大字符count和最小字符count的差.
这个没什么可优化的, 简单的先处理下substring的count方法即可.
class Solution {
public int beautySum(String s) {
int[] count = new int[26];
int res = 0;
for(int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
for(int j = i + 1; j < s.length(); j++) {
count[s.charAt(j) - 'a']++;
res += find(count);
}
count = new int[26];
}
return res;
}
public int find(int[] count) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int c : count){
if(c == 0)
continue;
min = Math.min(min, c);
max = Math.max(max, c);
}
return max - min;
}
}