-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateParanthesis.java
More file actions
32 lines (29 loc) · 935 Bytes
/
DuplicateParanthesis.java
File metadata and controls
32 lines (29 loc) · 935 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
package Stacks;
import java.util.Stack;
public class DuplicateParanthesis {
public static void main(String[] args) {
String exp1 = "((a+b)+(c+d))"; // false
String exp2 = "((a+b))"; // true
System.out.println(duplicateParanthesis(exp1));
System.out.println(duplicateParanthesis(exp2));
}
private static boolean duplicateParanthesis(String s) {
Stack <Character> stack = new Stack<>();
int n = s.length();
for (int i=0; i<n; i++){
char ch = s.charAt(i);
if (ch == ')'){
int count = 0;
while (stack.peek() != '('){
count++;
stack.pop();
}
if (count < 1) return true;
else stack.pop();
}else{
stack.push(ch);
}
}
return false;
}
}