#C7416. Longest Valid Parentheses Sequence Length
Longest Valid Parentheses Sequence Length
Longest Valid Parentheses Sequence Length
Given a string containing only the characters ('
and ')
, determine the maximum length of a valid parentheses sequence that can be formed by rearranging its characters.
A valid parentheses sequence is one where every opening parenthesis has a corresponding closing parenthesis. If the count of '(' is x and the count of ')' is y, then the longest valid sequence that can be formed will have a length of \(2\times\min(x, y)\). This is because each pair consists of one '(' and one ')'.
Your task is to compute \(2\times\min(\text{number of } '(' , \text{number of } ')')\) and output the result.
inputFormat
A single string consisting solely of the characters '(' and ')'. The input is provided via standard input (stdin).
outputFormat
An integer representing the length of the longest valid parentheses sequence that can be formed, printed to standard output (stdout).## sample
(()())
6