We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionisBalanced($s){// Write your code here$stack=[];$left=["(","{","["];$right=[")","}","]"];$len=strlen($s);for($i=0;$i<$len;$i++){if(0===$i&&in_array($s[$i],$right)){return"NO";}if(in_array($s[$i],$left)){$stack[]=$s[$i];continue;}switch($s[$i]){case")":if($stack[count($stack)-1]!=="("){return"NO";}break;case"}":if($stack[count($stack)-1]!=="{"){return"NO";}break;case"]":if($stack[count($stack)-1]!=="["){return"NO";}break;}array_pop($stack);}returnempty($stack)?"YES":"NO";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
php solution