Skip to content

Commit 79c1934

Browse files
authored
Create 1781-sum-of-beauty-of-all-substrings.js
1 parent 6d19821 commit 79c1934

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const beautySum = function(s) {
6+
let ans = 0
7+
const a = 'a'.charCodeAt(0)
8+
const min = arr => {
9+
let res = Infinity
10+
for(let e of arr) {
11+
if(e !== 0) res = Math.min(e, res)
12+
}
13+
return res
14+
}
15+
for(let i = 0, n = s.length; i < n; i++) {
16+
let freq = Array(26).fill(0)
17+
for(let j = i; j < n; j++) {
18+
freq[s.charCodeAt(j) - a]++
19+
ans += Math.max(...freq) - min(freq)
20+
}
21+
}
22+
return ans
23+
};

0 commit comments

Comments
 (0)