Skip to content

Commit 2410f91

Browse files
add 1653
1 parent 3101b94 commit 2410f91

File tree

3 files changed

+86
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+86
-0
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
| 1658 | [Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | [Javascript](./javascript/_1658.js), [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1658.java) || Medium | Greedy |
152152
| 1657 | [Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1657.java) | [:tv:](https://youtu.be/-jXQK-UeChU) | Medium | Greedy |
153153
| 1656 | [Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1656.java) || Easy | Array, Design |
154+
| 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1653.java) || Medium | Array, DP, Stack |
154155
| 1652 | [Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1652.java) || Easy | Array |
155156
| 1650 | [Lowest Common Ancestor of a Binary Tree III](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1650.java) || Medium | HashTable, Binary Tree, Tree |
156157
| 1646 | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1646.java) || Easy | Array |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
public class _1653 {
7+
public static class Solution1 {
8+
public int minimumDeletions(String s) {
9+
int[][] dp = new int[s.length()][2];
10+
int count = 0;
11+
//we count the number of 'b's to the left of each index
12+
for (int i = 0; i < s.length(); i++) {
13+
dp[i][0] = count;
14+
if (s.charAt(i) == 'b') {
15+
count++;
16+
}
17+
}
18+
count = 0;
19+
//now we count the number of 'a's to the left of each index
20+
for (int i = s.length() - 1; i >= 0; i--) {
21+
dp[i][1] = count;
22+
if (s.charAt(i) == 'a') {
23+
count++;
24+
}
25+
}
26+
int deletions = s.length();
27+
//we can balance the string by deleting all 'b's to the left and all 'a's to the right at each index
28+
for (int i = 0; i < s.length(); i++) {
29+
deletions = Math.min(deletions, dp[i][0] + dp[i][1]);
30+
}
31+
return deletions;
32+
}
33+
}
34+
35+
public static class Solution2 {
36+
/**
37+
* use stack
38+
* whenever we encounter a "ba" pair, we increase deletions count by one
39+
*/
40+
public int minimumDeletions(String s) {
41+
Deque<Character> stack = new LinkedList<>();
42+
int deletions = 0;
43+
for (char c : s.toCharArray()) {
44+
if (!stack.isEmpty() && stack.peekLast() == 'b' && c == 'a') {
45+
stack.pollLast();
46+
deletions++;
47+
} else {
48+
stack.addLast(c);
49+
}
50+
}
51+
return deletions;
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.solutions.secondthousand._1653;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _1653Test {
10+
private static _1653.Solution1 solution1;
11+
private static _1653.Solution2 solution2;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _1653.Solution1();
16+
solution2 = new _1653.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(2, solution1.minimumDeletions("aababbab"));
22+
assertEquals(2, solution2.minimumDeletions("aababbab"));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(0, solution1.minimumDeletions("aaabbb"));
28+
assertEquals(0, solution2.minimumDeletions("aaabbb"));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)