-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1653.java
55 lines (52 loc) · 1.78 KB
/
_1653.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.fishercoder.solutions.secondthousand;
import java.util.Deque;
import java.util.LinkedList;
public class _1653 {
public static class Solution1 {
public int minimumDeletions(String s) {
int[][] dp = new int[s.length()][2];
int count = 0;
// we count the number of 'b's to the left of each index
for (int i = 0; i < s.length(); i++) {
dp[i][0] = count;
if (s.charAt(i) == 'b') {
count++;
}
}
count = 0;
// now we count the number of 'a's to the left of each index
for (int i = s.length() - 1; i >= 0; i--) {
dp[i][1] = count;
if (s.charAt(i) == 'a') {
count++;
}
}
int deletions = s.length();
// we can balance the string by deleting all 'b's to the left and all 'a's to the right
// at each index
for (int i = 0; i < s.length(); i++) {
deletions = Math.min(deletions, dp[i][0] + dp[i][1]);
}
return deletions;
}
}
public static class Solution2 {
/*
* use stack
* whenever we encounter a "ba" pair, we increase deletions count by one
*/
public int minimumDeletions(String s) {
Deque<Character> stack = new LinkedList<>();
int deletions = 0;
for (char c : s.toCharArray()) {
if (!stack.isEmpty() && stack.peekLast() == 'b' && c == 'a') {
stack.pollLast();
deletions++;
} else {
stack.addLast(c);
}
}
return deletions;
}
}
}