-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (47 loc) · 1.23 KB
/
main.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
54
55
56
57
58
59
package main
import "fmt"
var parentheses = map[byte]byte{
'(': ')',
'[': ']',
'{': '}',
}
func isValid(s string) bool {
// empty is always valid
if s == "" {
return true
}
// can not be valid of only one char
if len(s) == 1 {
return false
}
// we're going to use stack to store the expected parentheses
var stack []byte
for i := range s { // Time: O(n) where n is number of chars in the string
// get the char
ch := s[i]
// check if that's the open, then add to stack, what is expected next
if ch == '(' || ch == '[' || ch == '{' {
stack = append(stack, parentheses[ch])
} else {
ln := len(stack)
// we have nothing to expect now?
if ln == 0 {
return false
}
// for close parentheses we need to pop the stack and compare if that the one we expect
expected := stack[ln-1]
stack = stack[:ln-1]
// not what we expect
if ch != expected {
return false
}
}
}
// it's valid only of everything consumed from the stack
return len(stack) == 0
}
func main() {
fmt.Printf("Parentheses sequense ()[]{} is valud: %t\n", isValid("()[]{}"))
fmt.Printf("Parentheses sequense ([]) is valud: %t\n", isValid("([])"))
fmt.Printf("Parentheses sequense (] is valud: %t\n", isValid("(]"))
}