#K66767. Longest Valid Parentheses

    ID: 32494 Type: Default 1000ms 256MiB

Longest Valid Parentheses

Longest Valid Parentheses

You are given a string s consisting solely of the characters '(' and ')'. Your task is to compute the length of the longest substring of s which is a valid (well-formed) parentheses sequence.

A valid parentheses string is defined as follows:

  • An empty string is valid.
  • If A is valid, then (A) is valid.
  • If A and B are valid, then their concatenation AB is also valid.

In mathematical terms, a string is valid if it obeys the following relation using the balance function b(i), which is the difference between the number of '(' and ')' in the prefix of length i. Formally, the string is valid if
\(b(i) \ge 0 \) for all \(i\) and \(b(n) = 0\), where \(n\) is the length of the string.

inputFormat

The input consists of a single line containing a string s that only includes the characters '(' and ')'.

outputFormat

Output a single integer, the length of the longest valid (well-formed) parentheses substring.## sample

(()
2

</p>