$handle = fopen ("php://stdin","r"); fscanf($handle,"%d",$t); for($a0 = 0; $a0 < $t; $a0++){ fscanf($handle,"%s",$str); $stack = array(); $unevenFlag = false; $matchingP = array( '}' => '{', ']' => '[', ')' => '(' ); $closingP = array_keys($matchingP); $openingP = array_values($matchingP); for ($i=0; $i < strlen($str); $i++) { if (in_array($str[$i], $openingP)) { array_push($stack, $str[$i]); } elseif (in_array($str[$i], $closingP)) { $openP = array_pop($stack); if ($matchingP[$str[$i]] != $openP) { $unevenFlag = true; break; } } } if (count($stack) != 0 || $unevenFlag === true) { print "NO\n"; } else { print "YES\n"; } }