-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate_parentheses.java
More file actions
36 lines (30 loc) · 873 Bytes
/
duplicate_parentheses.java
File metadata and controls
36 lines (30 loc) · 873 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
import java.util.*;
public class duplicate_parentheses{
public static boolean check_duplicate(String s){
Stack<Character> stack=new Stack<>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c==')'){
int count=0;
while(stack.peek()!='('){
stack.pop();
count++;
}
if(count!=0){
stack.pop();
return false;
}
}
else{
stack.push(c);
}
}
return true;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("enter a expression : ");
String s=sc.next();
System.out.println(check_duplicate(s));
}
}