#P1944. Longest Valid Bracket Substring
Longest Valid Bracket Substring
Longest Valid Bracket Substring
Given a string consisting only of the characters (
, )
, [
and ]
, find the length of its longest contiguous substring that is a valid bracket matching sequence.
A bracket matching sequence is defined as follows:
()
and[]
are valid sequences.- If A is a valid sequence, then
(A)
and[A]
are also valid sequences. - If both A and B are valid sequences, then their concatenation
AB
is also a valid sequence.
A substring of a string is a contiguous block of characters from that string. The empty string is also considered a substring.
Your task is to compute the length of the longest substring that is a valid bracket matching sequence.
Note: If no non-empty valid sequence exists, output 0.
inputFormat
The input consists of a single line containing a string composed only of the characters ('
, ')'
, '['
and ']'
.
outputFormat
Output a single integer representing the length of the longest valid bracket substring.
sample
([])
4