Skip to content

Instantly share code, notes, and snippets.

@luzeduardo
Created November 6, 2016 11:24
Show Gist options
  • Select an option

  • Save luzeduardo/619bfc7832f32573b199b5558fb673f2 to your computer and use it in GitHub Desktop.

Select an option

Save luzeduardo/619bfc7832f32573b199b5558fb673f2 to your computer and use it in GitHub Desktop.
function solution($S) {
$len = strlen($S);
if(!$len){
return 0;
}
$data = str_split($S, 1);
$datasize = count($data) + 1;
$chunked = array_chunk($data, $datasize / 2);
$chunkedl[] = $chunked[0];
$l = array_reduce($chunkedl, function ($total, $entry) {
$rmatches = preg_grep('/^(\(+)$/', $entry);
return $total += $rmatches ? count($rmatches) : 0;
});
$chunkedr[] = $chunked[1];
$r = array_reduce($chunkedr, function ($total, $entry) {
$lmatches = preg_grep('/^(\)+)$/', $entry);
return $total += $lmatches ? count($lmatches) : 0;
});
if($r > $l){
return $l;
} elseif($r < $l) {
return $r;
}
return $r;
}
var_dump(
solution("((()) )(())")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment