We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e5bd56e commit 66bdce5Copy full SHA for 66bdce5
Medium/Sum of Beauty of All Substrings.java
@@ -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