-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMinimumBracketsReversal.java
More file actions
78 lines (67 loc) · 2.61 KB
/
MinimumBracketsReversal.java
File metadata and controls
78 lines (67 loc) · 2.61 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package stacks.Assignment;
import java.util.Stack;
/*For a given expression in the form of a string, find the minimum number of brackets that can be reversed in order to make the expression balanced. The expression will only contain curly brackets.
If the expression can't be balanced, return -1.
Example:
Expression: {{{{
If we reverse the second and the fourth opening brackets, the whole expression will get balanced. Since we have to reverse two brackets to make the expression balanced, the expected output will be 2.
Expression: {{{
In this example, even if we reverse the last opening bracket, we would be left with the first opening bracket and hence will not be able to make the expression balanced and the output will be -1.
Input Format :
The first and the only line of input contains a string expression, without any spaces in between.
Output Format :
The only line of output will print the number of reversals required to balance the whole expression. Prints -1, otherwise.
Note:
You don't have to print anything. It has already been taken care of.
Constraints:
0 <= N <= 10^6
Where N is the length of the expression.
Time Limit: 1sec
Sample Input 1:
{{{
Sample Output 1:
-1
Sample Input 2:
{{{{}}
Sample Output 2:
1*/
public class MinimumBracketsReversal {
public static int countBracketsReversals(String expression) {
if (expression.length() == 0) {
return 0;
}
if (expression.length() % 2 != 0) {
return -1; // Only even numbers of brackets can be balanced
}
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < expression.length(); i++) {
char currentCharacter = expression.charAt(i);
if (currentCharacter == '{') {
stack.push(currentCharacter);
} else {
// pop if there is a balanced pair
if (!stack.isEmpty() && stack.peek() == '{') {
stack.pop();
} else {
stack.push(currentCharacter);
}
}
}
int count = 0;
// only unbalanced brackets are there in stack now
while (!stack.isEmpty()) {
char char1 = stack.pop();
char char2 = stack.pop();
/*when char1 = } and char2 = {, then we need to reverse both of them so count will increase by 2*/
if (char1 != char2) {
count += 2;
} else {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(countBracketsReversals("{{{{}}"));
}
}