-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisValid.go
More file actions
26 lines (24 loc) · 724 Bytes
/
Copy pathisValid.go
File metadata and controls
26 lines (24 loc) · 724 Bytes
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
/* https://leetcode.com/problems/valid-parentheses/#/description
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
*/
package lstring
func isValid(s string) bool {
var stack []byte
for i := 0; i < len(s); i++ {
v := s[i]
if v == '(' || v == '[' || v == '{' {
stack = append(stack, v)
continue
}
if len(stack) == 0 {
return false
}
v1 := stack[len(stack)-1]
if (v == ')' && v1 != '(') || (v == ']' && v1 != '[') || (v == '}' && v1 != '{') {
return false
}
stack = stack[:len(stack)-1]
}
return len(stack) == 0
}