From 977b24d3364a09a594d0bbb243f26e5aaf541487 Mon Sep 17 00:00:00 2001 From: guimalonso Date: Sun, 17 Oct 2021 23:00:05 -0300 Subject: [PATCH] feat: add valid-parenthesis solution in php --- .../valid-parenthesis.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 leetcode/20 - validparenthesis/valid-parenthesis.php diff --git a/leetcode/20 - validparenthesis/valid-parenthesis.php b/leetcode/20 - validparenthesis/valid-parenthesis.php new file mode 100644 index 0000000..e3feff9 --- /dev/null +++ b/leetcode/20 - validparenthesis/valid-parenthesis.php @@ -0,0 +1,39 @@ + '(', + ']' => '[', + '}' => '{' + ]; + + for ($index = 0; $index < strlen($s); $index++) { + $character = $s[$index]; + switch ($character) { + case '(': + case '[': + case '{': + $stack[] = $character; + break; + case ')': + case ']': + case '}': + if (count($stack) === 0 || $stack[count($stack)-1] !== $symbols[$character]) { + return false; + } + + array_pop($stack); + break; + } + } + + return count($stack) === 0; + } +} \ No newline at end of file