题目:只有[和(两种括号出现,判断一个字符串是否是括号匹配的
import java.util.*;
public class Main2{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()){
int N=sc.nextInt();
String[] strs=new String[N];
for(int i=0;i<N;i++){
strs[i]=sc.next();
if(IsOk(strs[i])){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
}
public static boolean IsOk(String str){
Stack<Character> s=new Stack<Character>();
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
if(s.empty()){
s.push(c);
}else if(s.peek()=='(' && c==')'){
s.pop();
}else if(s.peek()=='[' && c==']'){
s.pop();
}else{
s.push(c);
}
}
if(!s.empty()){
return false;
}else{
return true;
}
}
}