Skip to content

Commit 854b399

Browse files
authored
Update valid-parenthesis-string.py
1 parent cdfc19e commit 854b399

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

Python/valid-parenthesis-string.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@ def checkValidString(self, s):
99
"""
1010
lower, upper = 0, 0 # keep lower bound and upper bound of '(' counts
1111
for c in s:
12-
if c == '(':
13-
lower += 1
14-
upper += 1
15-
elif c == ')':
16-
if upper == 0: # '(' count is not enough
17-
return False
18-
lower = max(lower-1, 0);
19-
upper -= 1
20-
else:
21-
lower = max(lower-1, 0);
22-
upper += 1
12+
lower += 1 if c == '(' else -1
13+
upper -= 1 if c == ')' else -1
14+
if upper < 0: break
15+
lower = max(lower, 0)
2316
return lower == 0 # range of '(' count is valid
24-

0 commit comments

Comments
 (0)