#K50377. Valid Parentheses
Valid Parentheses
Valid Parentheses
Given a string consisting solely of the characters '(' and ')', determine whether the parentheses are properly nested and balanced.
A string is considered balanced if for every opening parenthesis there is a corresponding closing parenthesis in the correct order. Formally, let \( s \) be a string of length \( n \). The string is valid if:
$$\sum_{i=1}^{n} \mathbb{1}(s_i = '(') = \sum_{i=1}^{n} \mathbb{1}(s_i = ')')$$
and for every prefix \( s[1\ldots k] \), it holds that
$$\sum_{i=1}^{k} \mathbb{1}(s_i = '(') \ge \sum_{i=1}^{k} \mathbb{1}(s_i = ')')$$
inputFormat
The input consists of a single line which is a non-empty string composed only of the characters '(' and ')'.
outputFormat
Output a single boolean value: "True" if the string is balanced, and "False" otherwise.
## sample()
True