lo
and hi
to 0. These variables represent the lowest and highest possible number of open parentheses.c
in the string s
.
c
is '(', increment both lo
and hi
.c
is ')', decrement both lo
and hi
.c
is '*', increment hi
but decrement lo
only if lo
is greater than 0 (since *
can be an empty string).hi
is less than 0, return false
as we have more closing parentheses than opening ones.lo
never goes below 0 as we cannot have a negative number of open parentheses.lo
is 0. If it is, return true
, indicating that the string can be a valid sequence of parentheses. Otherwise, return false
.