#C4507. Balanced Parentheses Checker
Balanced Parentheses Checker
Balanced Parentheses Checker
Given a string representing a mathematical expression containing digits and the operators +
, -
, *
, /
along with parentheses, determine whether the parentheses in the expression are balanced.
An expression is said to be balanced if every opening parenthesis (
has a corresponding closing parenthesis )
. More formally, an expression is balanced if it satisfies the following condition:
\(\forall\, \text{prefix } P \text{ of the expression, } \#\text{(} \geq \#\text{)}\text{, and } \#\text{(} = \#\text{)}\)
Note that the expression contains only the characters (
, )
, +
, -
, *
, /
and digits.
inputFormat
A single line containing a mathematical expression. The expression may include digits, (
, )
, and the operators +
, -
, *
, /
.
outputFormat
Output a single line: True
if the expression has balanced parentheses, and False
otherwise.## sample
3 + (2 * (1 + 3) / 2)
True