- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBracketMatcher.java
46 lines (34 loc) · 947 Bytes
/
BracketMatcher.java
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
importjava.util.*;
importjava.io.*;
classMain {
publicstaticStringBracketMatcher(Stringstr) {
if(!str.contains("(") && !str.contains(")"))
return"1";
Stack<String> stack = newStack<String>();
String[] strArray = str.split("");
for(inti=0; i < strArray.length; i++){
Stringx = strArray[i];
if(x.equals("("))
stack.push(x);
if(x.equals(")")){
if(stack.isEmpty())
return"0";
Stringy = stack.peek();
if(x.equals(")") && y.equals("(")){
stack.pop();
}else{
return"0";
}
}
}
if(stack.isEmpty())
return"1";
else
return"0";
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(BracketMatcher(s.nextLine()));
}
}