Skip to content

Commit ea18a89

Browse files
committed
Create 0678-valid-parenthesis-string.go
1 parent c6de535 commit ea18a89

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

go/0678-valid-parenthesis-string.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func checkValidString(s string) bool {
2+
minLeft, maxLeft := 0, 0
3+
4+
for i := range s {
5+
if s[i] == '(' {
6+
minLeft++
7+
maxLeft++
8+
} else if s[i] == ')' {
9+
minLeft--
10+
maxLeft--
11+
} else {
12+
minLeft--
13+
maxLeft++
14+
}
15+
if maxLeft < 0 {
16+
return false
17+
}
18+
if minLeft < 0 {
19+
minLeft = 0
20+
}
21+
}
22+
return minLeft == 0
23+
}

0 commit comments

Comments
 (0)