-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_21.java
More file actions
57 lines (46 loc) · 1.77 KB
/
Problem_21.java
File metadata and controls
57 lines (46 loc) · 1.77 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
package strings;
import java.util.Stack;
//* Minimum number of bracket reversals needed to make an expression balanced.
public class Problem_21 {
public static int countMinReversals(String expr) {
if (expr.length() % 2 != 0) { // Odd length expressions cannot be balanced
return -1;
}
int openCount = 0;
int closeCount = 0;
Stack<Character> stack = new Stack<>();
for (char c : expr.toCharArray()) {
if (c == '{') {
openCount++;
stack.push(c);
} else if (c == '}') {
closeCount++;
if (!stack.isEmpty() && stack.peek() == '{') {
stack.pop();
} else {
// Unbalanced closing bracket, need reversal
openCount++;
}
}
}
// Calculate remaining unbalanced brackets
int unbalanced = Math.abs(openCount - closeCount);
return (unbalanced + 1) / 2; // Need to reverse half of the remaining unbalanced brackets
}
public static void main(String[] args) {
String expression1 = "{([)]}";
String expression2 = "[(])";
int reversals1 = countMinReversals(expression1);
int reversals2 = countMinReversals(expression2);
if (reversals1 != -1) {
System.out.println(expression1 + " requires " + reversals1 + " reversals to be balanced.");
} else {
System.out.println(expression1 + " cannot be balanced.");
}
if (reversals2 != -1) {
System.out.println(expression2 + " requires " + reversals2 + " reversals to be balanced.");
} else {
System.out.println(expression2 + " cannot be balanced.");
}
}
}