#K42352. Valid Parentheses Checker
Valid Parentheses Checker
Valid Parentheses Checker
You are given an alphanumeric expression that may contain arithmetic operators (+, -, *, /), whitespace, and various types of parentheses: ( )
, { }
, and [ ]
. Your task is to determine whether the parentheses in the given expression are correctly and properly nested.
An expression is considered valid if:
- Every opening parenthesis has a corresponding closing parenthesis of the same type.
- The order of the parentheses is correct (i.e., they are well-nested).
- The expression does not contain any invalid characters. Valid characters include alphanumeric characters, the arithmetic operators +, -, *, /, and whitespace.
The validity condition can be expressed using the following rule in \( \LaTeX \):
\[ \text{An expression } E \text{ is valid if } \forall \text{ pairs } (x,y), \; \text{if } x \text{ is an opening bracket then there exists a corresponding closing bracket } y \text{ such that } E = \ldots x \ldots y \ldots \text{ and the brackets in between are also valid.} \]inputFormat
The input is given via stdin and consists of a single line containing the expression to be checked.
outputFormat
The output should be printed to stdout. Print "True" if the expression has valid nested parentheses and contains only allowed characters; otherwise, print "False".## sample
(a+b)[c*d]{e/f}
True