#C12835. Balanced Parentheses Checker
Balanced Parentheses Checker
Balanced Parentheses Checker
Problem Description
Given a string representing a mathematical expression, your task is to determine whether the expression has balanced parentheses. The expression may contain three types of brackets: (), [], and {}. An expression is considered balanced if every opening bracket has a corresponding closing bracket and the pairs of brackets are properly nested.
For example, the expression (a + b) * (c - d)
is balanced, but the expression (a + b * (c - d)
is not. Use the following LaTeX formatted explanation for balance check of parentheses:
\( \text{An expression is balanced if and only if every opening bracket \( \{\texttt{(, [, \{\texttt{\{} \} \} }\) is closed in the correct order.} \)
Write a program that reads the expression from standard input and prints True
if the expression is balanced, or False
otherwise.
inputFormat
The input consists of a single line containing the expression string. The string may contain spaces and other characters, but only the brackets (
, )
, [
, ]
, {
, and }
are relevant for the balance check.
outputFormat
Output a single line: True
if the expression has balanced parentheses, and False
otherwise.
(a + b) * (c - d)
True