53 lines
691 B
Go
53 lines
691 B
Go
package leetcode
|
|
|
|
type stack struct {
|
|
array [10000]string
|
|
index int
|
|
}
|
|
|
|
var m = map[string]string{
|
|
"}": "{",
|
|
")": "(",
|
|
"]": "[",
|
|
}
|
|
|
|
func (s *stack) push(r string) bool {
|
|
switch r {
|
|
case "(":
|
|
fallthrough
|
|
case "{":
|
|
fallthrough
|
|
case "[":
|
|
s.array[s.index] = r
|
|
s.index++
|
|
case ")":
|
|
fallthrough
|
|
case "}":
|
|
fallthrough
|
|
case "]":
|
|
if s.index == 0 {
|
|
return false
|
|
}
|
|
s.index = s.index - 1
|
|
o := m[r]
|
|
if o != s.array[s.index] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *stack) empty() bool {
|
|
return s.index == 0
|
|
}
|
|
|
|
func isValid(s string) bool {
|
|
st := &stack{}
|
|
for i := 0; i < len(s); i++ {
|
|
if !st.push(s[i : i+1]) {
|
|
return false
|
|
}
|
|
}
|
|
return st.empty()
|
|
}
|