Skip to content

Commit

Permalink
Update valid-parenthesis-string.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 17, 2017
1 parent 854b399 commit 01411d1
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions C++/valid-parenthesis-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ class Solution {
bool checkValidString(string s) {
int lower = 0, upper = 0; // keep lower bound and upper bound of '(' counts
for (const auto& c : s) {
if (c == '(') {
++lower;
++upper;
} else if (c == ')') {
if (upper == 0) { // '(' count is not enough
return false;
}
lower = max(lower - 1, 0);
--upper;
} else {
lower = max(lower - 1, 0);
++upper;
}
lower += (c == '(') ? 1 : -1;
upper -= (c == ')') ? 1 : -1;
if (upper < 0) break;
lower = max(lower, 0);
}
return lower == 0; // range of '(' count is valid
}
Expand Down

0 comments on commit 01411d1

Please sign in to comment.