Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active February 1, 2020 16:00
Show Gist options
  • Save anjanesh/14d04cc302a7c57a3a0151ad9a098eeb to your computer and use it in GitHub Desktop.
Save anjanesh/14d04cc302a7c57a3a0151ad9a098eeb to your computer and use it in GitHub Desktop.
<?php
$s = "((a+b*d)*(x/2))*(1+(y+(x-2)/10))";
$b = [];
$j = 0;
$stack = [];
for ($i = 0; $i < strlen($s); $i++)
{
if ($s[$i] == '(')
{
$b[++$j] = '';
}
elseif ($s[$i] == ')')
{
$stack[] = ($b[$j--] .= ')');
}
for ($k = 1; $k <= $j; $k++)
{
$b[$k] .= $s[$i];
}
}
echo $s.PHP_EOL;
print_r($stack);
echo PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment