Description:
Now, there is a row of parentheses, please check that the brackets are paired.
Enter:
The first line enters a number of n (0
Import Java.util.Scanner;
Import Java.util.Stack;
public class Demo01 {public static void main (string[] args) {Scanner sc = new Scanner (system.in);
int num = Sc.nextint ();
string[] str = new String[num];
for (int i = 0;i < num;i++) {Str[i] = Sc.next (); for (int i = 0;i<num;i++) {if (Matchinfo (str[i)) = = True) {System.out.println ("Yes
");
}else{System.out.println ("NO"); }} public static Boolean matchinfo (String str) {stack<character> Stack = new Stack<c
Haracter> ();
char[] CA = Str.tochararray (); if ((Character) ca[0]== ' [') | | ((Character) ca[0]== ' ('))
{for (int index=0;index<ca.length;index++) {Character C1 = (Character) Ca[index]; if (c1== ' [') | | (c1== ' ('))
{Stack.push (C1);
}else if (Stack.empty () ==true) {//test stack is empty return false; }else if ((c1== ']) | | (c1== ')) {//peek () view the object at the top of the stack, but does not remove it if (((Character) stack.peek ()) = = ' (') && Amp (C1 = =)) | | ((((Character) stack.peek ()) = = ' [') && (c1== ']))
{Stack.pop ()//Removes the value from the top of the stack and returns the object as the value of this function} else{
return false;
}}///test stack is empty and returns true if and only if no items are included in the stack, return false to Stack.empty ();
else return false;
}
}