From 621a67ee54f2b4ab0f7a871e1a2361d237541559 Mon Sep 17 00:00:00 2001 From: Syedalisait Date: Mon, 26 Sep 2022 10:47:34 +0400 Subject: [PATCH] Go: 20. Valid Parentheses --- go/20-Valid-Parentheses.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 go/20-Valid-Parentheses.go diff --git a/go/20-Valid-Parentheses.go b/go/20-Valid-Parentheses.go new file mode 100644 index 000000000..e6ddf7a68 --- /dev/null +++ b/go/20-Valid-Parentheses.go @@ -0,0 +1,22 @@ +func isValid(s string) bool { + stack := make([]rune, 0) + matchingBrackets := map[rune]rune{ + ')': '(', + ']': '[', + '}': '{', + } + for _, bracket := range s { + switch bracket { + case '(', '{', '[': + stack = append(stack, bracket) + case ')', '}', ']': + if len(stack) == 0 || stack[len(stack)-1] != matchingBrackets[bracket] { + return false + } + + stack = stack[:len(stack)-1] + } + } + + return len(stack) == 0 +} \ No newline at end of file