#C11383. Balanced Parentheses Checker
Balanced Parentheses Checker
Balanced Parentheses Checker
You are given a string s
that consists solely of the characters '(' and ')'. Your task is to determine whether the string is balanced.
A string is considered balanced if every opening parenthesis has a corresponding closing parenthesis and the pairs of parentheses are properly nested. In other words, at any point in the string, the number of closing parentheses should not exceed the number of opening parentheses.
The mathematical condition for balance can be expressed as: \( \forall i, \; balance(i) \ge 0 \) and \( balance(n) = 0 \), where \( balance(i) \) is the cumulative difference between '(' and ')' up to position \( i \), and \( n \) is the length of the string.
Read the input from stdin
and output the result to stdout
as either True
or False
.
inputFormat
The input consists of a single line, representing the string of parentheses s
. The string may be empty and will contain only the characters '(' and ')'.
Example:
()()
outputFormat
Output a single line: True
if the parentheses in the string are balanced, and False
otherwise.
Example:
True## sample
()()
True