File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,451 LeetCode solutions in JavaScript
1
+ # 1,452 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1273
1273
1648|[ Sell Diminishing-Valued Colored Balls] ( ./solutions/1648-sell-diminishing-valued-colored-balls.js ) |Medium|
1274
1274
1649|[ Create Sorted Array through Instructions] ( ./solutions/1649-create-sorted-array-through-instructions.js ) |Hard|
1275
1275
1652|[ Defuse the Bomb] ( ./solutions/1652-defuse-the-bomb.js ) |Easy|
1276
+ 1653|[ Minimum Deletions to Make String Balanced] ( ./solutions/1653-minimum-deletions-to-make-string-balanced.js ) |Medium|
1276
1277
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
1277
1278
1668|[ Maximum Repeating Substring] ( ./solutions/1668-maximum-repeating-substring.js ) |Easy|
1278
1279
1669|[ Merge In Between Linked Lists] ( ./solutions/1669-merge-in-between-linked-lists.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1653. Minimum Deletions to Make String Balanced
3
+ * https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a string s consisting only of characters 'a' and 'b'.
7
+ *
8
+ * You can delete any number of characters in s to make s balanced. s is balanced if there is
9
+ * no pair of indices (i,j) such that i < j and s[i] = 'b' and s[j]= 'a'.
10
+ *
11
+ * Return the minimum number of deletions needed to make s balanced.
12
+ */
13
+
14
+ /**
15
+ * @param {string } s
16
+ * @return {number }
17
+ */
18
+ var minimumDeletions = function ( s ) {
19
+ let aCountRight = 0 ;
20
+ for ( const char of s ) {
21
+ if ( char === 'a' ) aCountRight ++ ;
22
+ }
23
+
24
+ let bCountLeft = 0 ;
25
+ let result = aCountRight ;
26
+
27
+ for ( const char of s ) {
28
+ if ( char === 'a' ) aCountRight -- ;
29
+ else bCountLeft ++ ;
30
+ result = Math . min ( result , aCountRight + bCountLeft ) ;
31
+ }
32
+
33
+ return result ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments