Skip to content

Commit 6b71fc1

Browse files
committed
Add solution #1653
1 parent af9e612 commit 6b71fc1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,451 LeetCode solutions in JavaScript
1+
# 1,452 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1273,6 +1273,7 @@
12731273
1648|[Sell Diminishing-Valued Colored Balls](./solutions/1648-sell-diminishing-valued-colored-balls.js)|Medium|
12741274
1649|[Create Sorted Array through Instructions](./solutions/1649-create-sorted-array-through-instructions.js)|Hard|
12751275
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|
12761277
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12771278
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12781279
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
};

0 commit comments

Comments
 (0)