Created
November 6, 2016 11:24
-
-
Save luzeduardo/619bfc7832f32573b199b5558fb673f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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