Skip to content

Commit

Permalink
special case, check position for first ocurrencies (dont need to be r…
Browse files Browse the repository at this point in the history
…ecursive)
  • Loading branch information
Francisco Mat committed May 9, 2018
1 parent dc9f1a7 commit b882a32
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions index.php
Expand Up @@ -3,6 +3,7 @@
$text = $_GET['b1-b'];

function verifyBrackets($s) {
#simple counting
$open_t1 = substr_count($s, '(');
$open_t2 = substr_count($s, '{');
$open_t3 = substr_count($s, '[');
Expand All @@ -14,6 +15,28 @@ function verifyBrackets($s) {

if($open_t1!=$close_t1 || $open_t2!=$close_t2 || $open_t3!=$close_t3 )
$brackets_ok=false;

#special case, check position for first ocurrencies (dont need to be recursive)
if($open_t1 && $close_t1) {
$pt_o = strpos($s, '(');
$pt_c = strpos($s, ')');
if($pt_c<$pt_o)
$brackets_ok=false;
}

if($open_t2 && $close_t2) {
$pt_o = strpos($s, '{');
$pt_c = strpos($s, '}');
if($pt_c<$pt_o)
$brackets_ok=false;
}

if($open_t3 && $close_t3) {
$pt_o = strpos($s, '[');
$pt_c = strpos($s, ']');
if($pt_c<$pt_o)
$brackets_ok=false;
}
return $brackets_ok;
}
$brackets_ok=verifyBrackets($text);
Expand Down

0 comments on commit b882a32

Please sign in to comment.