-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (46 loc) · 893 Bytes
/
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
package main
func isValid(s string) bool {
temp := map[string]string{
"{": "}",
"(": ")",
"[": "]",
}
var stack []string
for i := 0; i < len(s); i++ {
if _, ok := temp[s[i:i+1]]; ok {
stack = append(stack, s[i:i+1])
continue
}
if len(stack) == 0 || temp[stack[len(stack)-1]] != s[i:i+1] {
return false
}
stack = stack[0 : len(stack)-1]
}
return len(stack) == 0
}
func isValid2(s string) bool {
temp := map[string]string{
"{": "}",
"(": ")",
"[": "]",
}
var stack []string
for i := 0; i < len(s); i++ {
if len(stack) == 0 {
if _, ok := temp[s[i:i+1]]; !ok {
return false
}
stack = append(stack, s[i:i+1])
continue
}
if _, ok := temp[s[i:i+1]]; ok {
stack = append(stack, s[i:i+1])
continue
}
if temp[stack[len(stack)-1]] != s[i:i+1] {
return false
}
stack = stack[0 : len(stack)-1]
}
return len(stack) == 0
}