#K11361. Longest Valid Parentheses and Brackets Substring

    ID: 23452 Type: Default 1000ms 256MiB

Longest Valid Parentheses and Brackets Substring

Longest Valid Parentheses and Brackets Substring

Given a string s consisting of the characters ('(', ')', '[', ']'), determine the length of the longest contiguous substring that is valid. A valid substring is defined such that every opening symbol has a matching closing symbol and the symbols are correctly nested.

The matching rules are as follows: $$\text{For every } '(' \text{ there must be a corresponding } ')'$$ and $$\text{for every } '[' \text{ there must be a corresponding } ']'$$. The order of the symbols must be maintained; that is, an opening symbol must appear before its matching closing symbol, and the pairs must be properly nested.

The first input line is an integer n which is the length of the string, followed by the string s itself on the next line.

inputFormat

The input is read from stdin and consists of two lines:

  1. The first line contains an integer n \( (1 \leq n \leq 10^6) \) indicating the length of the string.
  2. The second line contains the string s composed exclusively of the characters ('(', ')', '[', ']').

outputFormat

Output a single integer on stdout representing the length of the longest valid substring of s.

## sample
6
()[[()
4

</p>