#K49802. Longest Balanced Parentheses Substring

    ID: 28723 Type: Default 1000ms 256MiB

Longest Balanced Parentheses Substring

Longest Balanced Parentheses Substring

You are given a string s consisting solely of the characters (' and ')'. Your task is to determine the length of the longest substring that forms a valid (i.e., balanced) set of parentheses.

A substring is considered balanced if every opening parenthesis (' has a corresponding closing parenthesis ') and the pairs are properly nested. The solution should efficiently compute the result in O(n) time complexity.

Mathematically, if we define the length of a valid substring as \(L\), your goal is to compute the maximum \(L\) such that the substring is valid. For example, for the input string (()()), the entire string is balanced and its length is 6.

inputFormat

Input: A single line containing a string composed exclusively of the characters '(' and ')'. The input is provided via standard input (stdin).

outputFormat

Output: An integer representing the length of the longest balanced parentheses substring. The result should be printed to standard output (stdout).

## sample
(()())
6