-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum bracket Reversal.java
58 lines (44 loc) · 1.36 KB
/
Minimum bracket Reversal.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
56
57
58
import java.util.*;
public class Solution {
// Function returns -1 if brackets can't be balanced
public static int countBracketReversals(String expr){
// Write your code here
int len = expr.length();
// length of expression must be even to make
// it balanced by using reversals.
if (len%2 != 0)
return -1;
// After this loop, stack contains unbalanced
// part of expression, i.e., expression of the
// form "}}..}{{..{"
Stack<Character> s=new Stack<>();
for (int i=0; i<len; i++)
{
char c = expr.charAt(i);
if (c =='}' && !s.empty())
{
if (s.peek()=='{')
s.pop();
else
s.push(c);
}
else
s.push(c);
}
// Length of the reduced expression
// red_len = (m+n)
int red_len = s.size();
// count opening brackets at the end of
// stack
int n = 0;
while (!s.empty() && s.peek() == '{')
{
s.pop();
n++;
}
// return ceil(m/2) + ceil(n/2) which is
// actually equal to (m+n)/2 + n%2 when
// m+n is even.
return (red_len/2 + n%2);
}
}