-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-parentheses.go
53 lines (42 loc) · 1022 Bytes
/
valid-parentheses.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
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"github.com/dimaglushkov/solutions/ADS/stack"
)
// source: https://leetcode.com/problems/valid-parentheses/
func isValid(s string) bool {
st := stack.NewGenStack[int32]()
pairs := map[rune]rune{
'{': '}',
'(': ')',
'[': ']',
}
for _, c := range s {
if c == '[' || c == '{' || c == '(' {
st.Push(c)
} else if sc, ok := st.Pop(); ok || pairs[sc] != c {
return false
}
}
return st.Len() == 0
}
func main() {
// Example 1
var s1 string = "()"
fmt.Println("Expected: true Output: ", isValid(s1))
// Example 2
var s2 string = "()[]{}"
fmt.Println("Expected: true Output: ", isValid(s2))
// Example 3
var s3 string = "(]"
fmt.Println("Expected: false Output: ", isValid(s3))
// Example 4
var s4 string = "((()))"
fmt.Println("Expected: true Output: ", isValid(s4))
// Example 5
var s5 string = "())"
fmt.Println("Expected: false Output: ", isValid(s5))
// Example 6
var s6 string = "([{})]"
fmt.Println("Expected: false Output: ", isValid(s6))
}