-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Deletions_to_Make_String_Balanced.java
More file actions
47 lines (47 loc) · 1.17 KB
/
Minimum_Deletions_to_Make_String_Balanced.java
File metadata and controls
47 lines (47 loc) · 1.17 KB
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
import java.util.*;
public class Minimum_Deletions_to_Make_String_Balanced {
class Solution {
public int minimumDeletions(String s) {
int n = s.length();
int[] dp = new int[n];
if(s.charAt(n-1) == 'a')
{
dp[n-1] = 1;
}
else
{
dp[n-1] = 0;
}
for(int i = n-2 ; i>=0 ; i--)
{
if(s.charAt(i) == 'a')
{
dp[i] = dp[i+1] + 1;
}
else
{
dp[i] = dp[i+1] + 0;
}
}
int min = Integer.MAX_VALUE;
int b_count = 0;
for(int i = 0 ; i<n ; i++)
{
if(s.charAt(i) == 'b')
{
min = Math.min(min , dp[i] + b_count);
b_count++;
}
}
min = Math.min(min , b_count);
if(min == Integer.MAX_VALUE)
{
return 0;
}
else
{
return min;
}
}
}
}