-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraceMatching.java
40 lines (37 loc) · 1005 Bytes
/
braceMatching.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
package acm;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
public class braceMatching {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int count = in.nextInt();
in.nextLine();
while((count--)>0){
Stack kuohao = new Stack();
String line = in.nextLine();
char[] listOfItem = line.toCharArray();
for(int i=0;i<listOfItem.length;i++){
char item;
if((item = listOfItem[i])=='['||(item = listOfItem[i])=='(')
kuohao.push(item);
else if((item = listOfItem[i])==']'){
if(kuohao.isEmpty()||(char) kuohao.pop()!='['){
System.out.println("No");
System.exit(0);
}
}
else if((item = listOfItem[i])==')'){
if(kuohao.isEmpty()||(char) kuohao.pop()!='('){
System.out.println("No");
System.exit(0);
}
}
}
if(kuohao.isEmpty())
System.out.println("Yes");
else
System.out.println("No");
}
}
}