-
Notifications
You must be signed in to change notification settings - Fork 5
/
solution.go
45 lines (41 loc) · 891 Bytes
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"reflect"
)
func isValid(s string) bool {
pairs := map[byte]byte{
'(': ')', '{': '}', '[': ']',
}
var stk []byte
for i := 0; i < len(s); i++ {
if p, ok := pairs[s[i]]; ok {
stk = append(stk, p)
} else {
if len(stk) == 0 || stk[len(stk)-1] != s[i] {
return false
}
stk = stk[:len(stk)-1]
}
}
return len(stk) == 0
}
type Example struct {
s string
ans bool
}
func main() {
examples := []Example{
{"()", true},
{"()[]{}", true},
{"(]", false},
}
for i, example := range examples {
ans := isValid(example.s)
if reflect.DeepEqual(ans, example.ans) {
fmt.Printf("PASS: CASE %d\n", i)
} else {
fmt.Printf("FAIL: CASE %d\n", i)
}
}
}