From 01411d1cc47531d328179749a2ccc7a56c08074a Mon Sep 17 00:00:00 2001 From: kamyu Date: Sun, 17 Sep 2017 16:04:29 +0800 Subject: [PATCH] Update valid-parenthesis-string.cpp --- C++/valid-parenthesis-string.cpp | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/C++/valid-parenthesis-string.cpp b/C++/valid-parenthesis-string.cpp index 874103b2a..ffaa727b4 100644 --- a/C++/valid-parenthesis-string.cpp +++ b/C++/valid-parenthesis-string.cpp @@ -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 }