-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedundantBrackets.java
More file actions
42 lines (34 loc) · 1.01 KB
/
RedundantBrackets.java
File metadata and controls
42 lines (34 loc) · 1.01 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
package Stacks;
import java.util.Stack;
public class RedundantBrackets {
static boolean checkRedundancy(String s) {
Stack<Character> st = new Stack<>();
char[] str = s.toCharArray();
for (char ch : str) {
if (ch == ')') {
boolean flag = true;
while (!st.isEmpty() && st.peek() != '(') {
char top = st.pop();
if (top == '+' || top == '-' || top == '*' || top == '/') flag = false;
}
if (!st.isEmpty()) st.pop();
if (flag) return true;
} else {
st.push(ch);
}
}
return false;
}
static void findRedundant(String str) {
boolean ans = checkRedundancy(str);
if (ans) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
public static void main(String[] args) {
String str = "((a+b))";
findRedundant(str);
}
}