-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (68 loc) · 1.11 KB
/
main.go
File metadata and controls
80 lines (68 loc) · 1.11 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import "fmt"
/*
*
*
* validation string
*
* args :
* str slices []string
*
* return value:
* bool
*
*
*/
func isValid(str string) bool {
// firs check if length
// str is odd return false
// else check if length str is zero
// return true
if len(str)%2 != 0 {
return false
} else if len(str) == 0 {
return true
}
// make on map for check
// symbol
chars := map[rune]rune{
')': '(',
']': '[',
'}': '{',
}
// make one quest
var q []rune
for _, v := range str {
// check we get close
// symbol or not
if k, ok := chars[v]; ok {
// if len q is zero
// this means we have
// symbol close without
// symbol open
if len(q) < 1 {
return false
}
// if symbol is different
// like [ == }
// return false
if q[len(q)-1] != k {
return false
}
// else pop last char in quest
q = q[:len(q)-1]
continue
}
// if not we append to quest
q = append(q, v)
}
// now check len q
// if is not zero
// this means one open symbol
// in quest!
return len(q) == 0
}
func main() {
// very simple test
fmt.Println(isValid("([{[{()}]}])"))
}