-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathP19.java
More file actions
37 lines (29 loc) · 864 Bytes
/
P19.java
File metadata and controls
37 lines (29 loc) · 864 Bytes
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
package stack_and_queue;
import java.util.Stack;
// Expression Contain redundant bracket or not
public class P19 {
static boolean checkRedundancy(String s) {
Stack<Character> st = new Stack<>();
char[] str = s.toCharArray();
for(char ch : str) {
if(ch == ')') {
char top = st.peek();
st.pop();
boolean flag = true;
while(top != '(') {
if(top == '+' || top == '-' || top == '*' || top == '/' )
flag = false;
top = st.peek();
st.pop();
}
if(flag == true)
return true;
}
else
st.push(ch);
}
return false;
}
public static void main(String[] args) {
}
}