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
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.