#C264. Longest Balanced Parentheses Substring
Longest Balanced Parentheses Substring
Longest Balanced Parentheses Substring
You are given a string S consisting only of the characters (
and )
. Your task is to determine the length of the longest contiguous substring that forms a balanced (i.e., well-formed) parentheses sequence.
A balanced parentheses sequence is defined as one where every opening bracket (
has a corresponding closing bracket )
in the correct order. Mathematically, for any valid substring, if we denote the number of opening brackets as \(L\) and the number of closing brackets as \(R\), then the substring is balanced if \(L = R\) and it can be parsed correctly.
Input/Output Requirements: Read input from stdin
and write output to stdout
.
inputFormat
The input consists of a single line containing the string S (which only includes the characters (
and )
). The string may be empty.
outputFormat
Output a single integer which is the length of the longest balanced (well-formed) parentheses substring found in S.
## sample(()())
6