171. Longest Valid Parentheses
Challenge1000 ms256 MBSolved by 0%
Read one line of ( and ) and print the length of the longest CONTIGUOUS
substring that is balanced.
In )()()) the longest valid stretch is ()(), so the answer is 4.
Print 0 if there is no balanced substring at all.
Constraints - The line has between 0 and 100000 characters. - Only `(` and `)` appear.
Input
A single line containing only ( and ). It may be empty.
Output
Print one integer — the length of the longest balanced contiguous substring, or 0 if there is none.
Input)()())
Output4
Noteis the worked example, with junk on both sides
Input(()
Output2
Notehas an unmatched opening bracket before the valid part
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution