#C13133. 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 compute the length of the longest substring of s
that forms a valid (balanced) parentheses sequence.
A balanced parentheses string is defined as one in which each opening bracket (
has a corresponding closing bracket )
and the pairs of parentheses are properly nested. For example, the string (()())
is balanced and has a length of 6, while the string (()
is not completely balanced and the longest balanced substring is ()
with a length of 2.
The solution to this problem requires an efficient algorithm that can handle large inputs using a stack-based approach.
inputFormat
The input consists of a single line, which is the string s
composed exclusively of the characters '(' and ')'.
outputFormat
Output a single integer, which is the length of the longest balanced parentheses substring found in s
.
The result should be printed to stdout
.
(()())
6