#K48117. Longest Balanced Parentheses Substring

    ID: 28350 Type: Default 1000ms 256MiB

Longest Balanced Parentheses Substring

Longest Balanced Parentheses Substring

You are given a string s consisting of characters '(' and ')'. Your task is to find the length of the longest contiguous substring which is balanced. A substring is considered balanced if and only if:

  • The number of opening parentheses is equal to the number of closing parentheses.
  • The parentheses are correctly nested.

In mathematical terms, a substring s[l...r] is balanced if it satisfies the condition $$\#('(') = \#(')')$$ and the substring forms a valid parentheses sequence.

For example:

  • For s = ")()())", the answer is 4.
  • For s = "(()))(", the answer is 4.
  • For an empty string, the answer is 0.

Your program should read the input from standard input (stdin) and output the result to standard output (stdout).

inputFormat

The input consists of a single line containing the string s, which is composed only of the characters '(' and ')'.

outputFormat

Output a single integer, the length of the longest balanced substring.

## sample
)()())
4