Skip to content

Commit b2e6a95

Browse files
committed
refactor Valid Parentheses
1 parent d7dc01e commit b2e6a95

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

go/valid_parentheses.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
package main
33

44
func isValid(s string) bool {
5-
p := map[rune]rune{')': '(', '}': '{', ']': '['}
65
var stack []rune
7-
8-
for _, r := range s {
9-
v, ok := p[r]
10-
6+
brackets := map[rune]rune{')': '(', '}': '{', ']': '['}
7+
for _, bracket := range s {
8+
openBracket, ok := brackets[bracket]
119
if !ok {
12-
stack = append(stack, r)
10+
stack = append(stack, bracket)
1311
continue
1412
}
15-
16-
if len(stack) < 1 || stack[len(stack)-1] != v {
13+
if len(stack) == 0 || stack[len(stack)-1] != openBracket {
1714
return false
1815
}
1916
stack = stack[:len(stack)-1]
2017
}
21-
2218
return len(stack) == 0
2319
}

0 commit comments

Comments
 (0)