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