#C10363. Minimum Additions to Make Parentheses Valid
Minimum Additions to Make Parentheses Valid
Minimum Additions to Make Parentheses Valid
You are given a string s
composed only of the characters '(' and ')'. The task is to determine the minimum number of parentheses that must be added to make the string valid. A valid string is defined such that every opening parenthesis has a corresponding closing parenthesis and vice versa.
Mathematically, if we denote the number of unmatched opening parentheses as $\text{open\_needed}$ and unmatched closing parentheses as $\text{close\_needed}$, then the answer is given by:
[ \text{result} = \text{open_needed} + \text{close_needed} ]
Example: For input s = ")))((()"
, the output should be 2
.
inputFormat
The input consists of a single line containing the string s
(which only includes the characters '(' and ')').
outputFormat
The output is an integer representing the minimum number of parentheses needed to be added so that the string s
becomes valid.
()))((
4