File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
solution/1600-1699/1653.Minimum Deletions to Make String Balanced Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -435,6 +435,27 @@ function minimumDeletions(s: string): number {
435
435
}
436
436
```
437
437
438
+ #### JavaScript
439
+
440
+ ``` js
441
+ /**
442
+ * @param {string} s
443
+ * @return {number}
444
+ */
445
+ var minimumDeletions = function (s ) {
446
+ let ra = [... s].reduce ((acc , x ) => (x === ' a' ? acc + 1 : acc), 0 );
447
+ let lb = 0 ;
448
+
449
+ let ans = s .length ;
450
+ for (const ch of s) {
451
+ if (ch === ' a' ) ra-- ;
452
+ ans = Math .min (ans, lb + ra);
453
+ if (ch === ' b' ) lb++ ;
454
+ }
455
+ return ans;
456
+ };
457
+ ```
458
+
438
459
<!-- solution: start -->
439
460
440
461
### Solution 4: Stack
Original file line number Diff line number Diff line change @@ -433,6 +433,27 @@ function minimumDeletions(s: string): number {
433
433
}
434
434
```
435
435
436
+ #### JavaScript
437
+
438
+ ``` js
439
+ /**
440
+ * @param {string} s
441
+ * @return {number}
442
+ */
443
+ var minimumDeletions = function (s ) {
444
+ let ra = [... s].reduce ((acc , x ) => (x === ' a' ? acc + 1 : acc), 0 );
445
+ let lb = 0 ;
446
+
447
+ let ans = s .length ;
448
+ for (const ch of s) {
449
+ if (ch === ' a' ) ra-- ;
450
+ ans = Math .min (ans, lb + ra);
451
+ if (ch === ' b' ) lb++ ;
452
+ }
453
+ return ans;
454
+ };
455
+ ```
456
+
436
457
<!-- tabs: end -->
437
458
438
459
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var minimumDeletions = function ( s ) {
6
+ let ra = [ ...s ] . reduce ( ( acc , x ) => ( x === 'a' ? acc + 1 : acc ) , 0 ) ;
7
+ let lb = 0 ;
8
+
9
+ let ans = s . length ;
10
+ for ( const ch of s ) {
11
+ if ( ch === 'a' ) ra -- ;
12
+ ans = Math . min ( ans , lb + ra ) ;
13
+ if ( ch === 'b' ) lb ++ ;
14
+ }
15
+ return ans ;
16
+ } ;
You can’t perform that action at this time.
0 commit comments