#K62907. Validate Mathematical Expression Parentheses
Validate Mathematical Expression Parentheses
Validate Mathematical Expression Parentheses
In this problem, you are given a mathematical expression as a string. The expression may contain digits (0-9), basic arithmetic operators (+, -, *, /), whitespace characters, and the symbols ( )
, [ ]
, and { }
. Your task is to check whether the expression has all its parentheses, brackets, and braces properly matched and nested.
An expression is considered valid if:
- Every opening symbol (
(
,[
,{
) is closed by its corresponding closing symbol ()
,]
,}
) in the correct order. - No unmatched symbols remain.
Note that the expression does not need to be mathematically evaluated; you only need to verify the proper pairing of the symbols.
Example:
3 + (2 - (4 / 2) * [2 + {3 * 4}])
is valid, while 3 + (4 - 5]
is not.
inputFormat
The input consists of a single line representing the mathematical expression which may include digits, operators, whitespace, and the symbols ( )
, [ ]
, and { }
.
For example:
3 + (2 - (4 / 2) * [2 + {3 * 4}])
outputFormat
Output a single line: True
if the expression is valid (i.e. all brackets are correctly matched and nested), or False
otherwise.
3 + (2 - (4 / 2) * [2 + {3 * 4}])
True