Skip to content

Commit 66bdce5

Browse files
authored
Create Sum of Beauty of All Substrings.java
1 parent e5bd56e commit 66bdce5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int beautySum(String s) {
3+
int sum = 0;
4+
for (int i = 0; i < s.length(); i++) {
5+
int[] charFrequency = new int[26];
6+
for (int j = i; j < s.length(); j++) {
7+
charFrequency[s.charAt(j) - 'a']++;
8+
sum += getBeauty(charFrequency);
9+
}
10+
}
11+
return sum;
12+
}
13+
14+
private int getBeauty(int[] charFrequency) {
15+
int min = Integer.MAX_VALUE ;
16+
int max = Integer.MIN_VALUE;
17+
for (int i = 0; i < 26; i++) {
18+
if (charFrequency[i] == 0) {
19+
continue;
20+
}
21+
min = Math.min(min, charFrequency[i]);
22+
max = Math.max(max, charFrequency[i]);
23+
}
24+
return max - min;
25+
}
26+
}

0 commit comments

Comments
 (0)