3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Brace Balance Check * * Function which checks whether or not the braces in an array of strings are balanced * @param array $values an array of strings containing braces * @return array $values an array of strings containing YES or NO responses */ function braceBalanceCheck(array $values) : array { for ($i = 0; $i <= count($values) - 1; $i++) { $braces = str_split($values[$i]); $values[$i] = isValid($braces); } return $values; } function isValid(array $braces) : string { $available = ['{' => '}', '(' => ')', '[' => ']']; $opened = []; $closed = []; for ($i = 0; $i <= count($braces) - 1; $i++) { if (in_array($braces[$i], array_keys($available))) { $opened[] = $braces[$i]; continue; } $closed[] = $braces[$i]; } return match($braces, $available, $opened, $closed); } function match(array $braces, array $available, array $opened, array $closed) : string { $last_open = $opened[count($opened) - 1]; $opened = array_values($opened); $closed = array_values($closed); if (!testValidity($braces, $available, $last_open, $opened, $closed)) { return 'NO'; } if (count($opened) > 1) { match($braces, $available, array_slice($opened, 0, -1) , array_slice($closed, 1)); } return 'YES'; } function testValidity(array $braces, array $available, string $last_open, array $opened, array $closed) : bool { // Check for unopened braces $is_opened = in_array( array_flip($available)[$closed[0]], array_slice($braces, 0, array_search($closed[0], $braces)) ); if (!$is_opened) { return false; } // check if not nested properly if ($available[$last_open] !== $closed[0]) { // check if consecutive if ($available[$opened[0]] === $closed[0]) { return true; } return false; } // check if not consecutive if ($available[$opened[0]] !== $closed[0]) { // check if nested properly if ($available[$last_open] === $closed[0]) { return true; } return false; } return true; } var_dump(braceBalanceCheck(['{[()]}', '{[(]}', '{}()[]', '{]}[', '{}{{'])); // YES, NO, YES, NO
based on b1NYj
Output for 7.0.30 - 7.3.0alpha1
Notice: Undefined offset: 0 in /in/nSWgg on line 44 Notice: Undefined index: in /in/nSWgg on line 44 Notice: Undefined offset: 0 in /in/nSWgg on line 45 array(5) { [0]=> string(3) "YES" [1]=> string(2) "NO" [2]=> string(3) "YES" [3]=> string(2) "NO" [4]=> string(3) "YES" }
Output for hhvm-3.22.0
Notice: Undefined index: 0 in /in/nSWgg on line -1 Notice: Undefined index: in /in/nSWgg on line -1 Notice: Undefined index: 0 in /in/nSWgg on line -1 array(5) { [0]=> string(3) "YES" [1]=> string(2) "NO" [2]=> string(3) "YES" [3]=> string(2) "NO" [4]=> string(3) "YES" }
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined index: 0 in /in/nSWgg on line 44 Notice: Undefined index: in /in/nSWgg on line 44 Notice: Undefined index: 0 in /in/nSWgg on line 45 array(5) { [0]=> string(3) "YES" [1]=> string(2) "NO" [2]=> string(3) "YES" [3]=> string(2) "NO" [4]=> string(3) "YES" }
Output for 5.6.30
Parse error: syntax error, unexpected ':', expecting '{' in /in/nSWgg on line 9
Process exited with code 255.