PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function checkIfBalanced ($string) { $stack = []; $startSymbols = '('; $pairs = '()'; $len = strlen($string); for ($i = 0; $i < $len; $i++) { $curr = $string[$i]; if ($curr === $startSymbols) { array_push($stack, $curr); print_r($stack); } else { $prev = array_pop($stack); $pair = "{$prev}{$curr}"; if ($pair != $pairs) { //return false; } } } print_r($stack); return count($stack) === 0; } $t = checkIfBalanced ('(5 + 6) * (7 + 8)/(4 + 3)'); //print_r($t);
Show:  
Copy Clear