File tree Expand file tree Collapse file tree 4 files changed +128
-0
lines changed
solution/1700-1799/1781.Sum of Beauty of All Substrings Expand file tree Collapse file tree 4 files changed +128
-0
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,52 @@ func beautySum(s string) (ans int) {
161
161
}
162
162
```
163
163
164
+ #### TypeScript
165
+
166
+ ``` ts
167
+ function beautySum(s : string ): number {
168
+ let ans = 0 ;
169
+ for (let i = 0 ; i < s .length ; ++ i ) {
170
+ const cnt = new Map ();
171
+ for (let j = i ; j < s .length ; ++ j ) {
172
+ cnt .set (s [j ], (cnt .get (s [j ]) || 0 ) + 1 );
173
+ const t = Array .from (cnt .values ());
174
+ ans += Math .max (... t ) - Math .min (... t );
175
+ }
176
+ }
177
+ return ans ;
178
+ }
179
+ ```
180
+
181
+ #### Rust
182
+
183
+ ``` rust
184
+ impl Solution {
185
+ pub fn beauty_sum (s : String ) -> i32 {
186
+ let mut ans = 0 ;
187
+ let n = s . len ();
188
+ let s : Vec <char > = s . chars (). collect ();
189
+
190
+ for i in 0 .. n {
191
+ let mut cnt = vec! [0 ; 26 ];
192
+ for j in i .. n {
193
+ cnt [s [j ] as usize - 'a' as usize ] += 1 ;
194
+ let mut mi = 1000 ;
195
+ let mut mx = 0 ;
196
+ for & v in & cnt {
197
+ if v > 0 {
198
+ mi = mi . min (v );
199
+ mx = mx . max (v );
200
+ }
201
+ }
202
+ ans += mx - mi ;
203
+ }
204
+ }
205
+ ans
206
+ }
207
+ }
208
+ ```
209
+
164
210
#### JavaScript
165
211
166
212
``` js
Original file line number Diff line number Diff line change @@ -159,6 +159,52 @@ func beautySum(s string) (ans int) {
159
159
}
160
160
```
161
161
162
+ #### TypeScript
163
+
164
+ ``` ts
165
+ function beautySum(s : string ): number {
166
+ let ans = 0 ;
167
+ for (let i = 0 ; i < s .length ; ++ i ) {
168
+ const cnt = new Map ();
169
+ for (let j = i ; j < s .length ; ++ j ) {
170
+ cnt .set (s [j ], (cnt .get (s [j ]) || 0 ) + 1 );
171
+ const t = Array .from (cnt .values ());
172
+ ans += Math .max (... t ) - Math .min (... t );
173
+ }
174
+ }
175
+ return ans ;
176
+ }
177
+ ```
178
+
179
+ #### Rust
180
+
181
+ ``` rust
182
+ impl Solution {
183
+ pub fn beauty_sum (s : String ) -> i32 {
184
+ let mut ans = 0 ;
185
+ let n = s . len ();
186
+ let s : Vec <char > = s . chars (). collect ();
187
+
188
+ for i in 0 .. n {
189
+ let mut cnt = vec! [0 ; 26 ];
190
+ for j in i .. n {
191
+ cnt [s [j ] as usize - 'a' as usize ] += 1 ;
192
+ let mut mi = 1000 ;
193
+ let mut mx = 0 ;
194
+ for & v in & cnt {
195
+ if v > 0 {
196
+ mi = mi . min (v );
197
+ mx = mx . max (v );
198
+ }
199
+ }
200
+ ans += mx - mi ;
201
+ }
202
+ }
203
+ ans
204
+ }
205
+ }
206
+ ```
207
+
162
208
#### JavaScript
163
209
164
210
``` js
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn beauty_sum ( s : String ) -> i32 {
3
+ let mut ans = 0 ;
4
+ let n = s. len ( ) ;
5
+ let s: Vec < char > = s. chars ( ) . collect ( ) ;
6
+
7
+ for i in 0 ..n {
8
+ let mut cnt = vec ! [ 0 ; 26 ] ;
9
+ for j in i..n {
10
+ cnt[ s[ j] as usize - 'a' as usize ] += 1 ;
11
+ let mut mi = 1000 ;
12
+ let mut mx = 0 ;
13
+ for & v in & cnt {
14
+ if v > 0 {
15
+ mi = mi. min ( v) ;
16
+ mx = mx. max ( v) ;
17
+ }
18
+ }
19
+ ans += mx - mi;
20
+ }
21
+ }
22
+ ans
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ function beautySum ( s : string ) : number {
2
+ let ans = 0 ;
3
+ for ( let i = 0 ; i < s . length ; ++ i ) {
4
+ const cnt = new Map ( ) ;
5
+ for ( let j = i ; j < s . length ; ++ j ) {
6
+ cnt . set ( s [ j ] , ( cnt . get ( s [ j ] ) || 0 ) + 1 ) ;
7
+ const t = Array . from ( cnt . values ( ) ) ;
8
+ ans += Math . max ( ...t ) - Math . min ( ...t ) ;
9
+ }
10
+ }
11
+ return ans ;
12
+ }
You can’t perform that action at this time.
0 commit comments