LeetCode: Sum Of Beauty Of All Substrings Solution

Approach

Brute-force

Implementation

1/**
2 * @param {string} s
3 * @return {number}
4 */
5var beautySum = function (s) {
6 const N = s.length
7 let sum = 0
8 for (let i = 0; i < N; i++) {
9 const frequency = Array(26).fill(0)
10 for (let j = i; j < N; j++) {
11 frequency[s[j].charCodeAt(0) - "a".charCodeAt(0)]++
12 sum +=
13 Math.max.apply(null, frequency) -
14 Math.min.apply(null, frequency.filter(Boolean))
15 }
16 }
17 return sum
18}

Comments

Loading comments...

Tags

leetcode

hash table

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Check If Number Is A Sum Of Powers Of Three

HoningJS

Search Posts