Skip to content

Commit 6a32b21

Browse files
committed
Create 0678-valid-parenthesis-string.swift
1 parent 0beb8f5 commit 6a32b21

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
func checkValidString(_ s: String) -> Bool {
3+
var leftMin = 0
4+
var leftMax = 0
5+
6+
for c in s {
7+
if c == "(" {
8+
leftMin += 1
9+
leftMax += 1
10+
} else if c == ")" {
11+
leftMin -= 1
12+
leftMax -= 1
13+
} else {
14+
leftMin -= 1
15+
leftMax += 1
16+
}
17+
18+
if leftMax < 0 {
19+
return false
20+
}
21+
22+
if leftMin < 0 {
23+
leftMin = 0
24+
}
25+
}
26+
27+
return leftMin == 0
28+
}
29+
}

0 commit comments

Comments
 (0)